wp_update_post - 使用自定义字段更新自定义post_type

时间:2018-03-30 12:36:17

标签: php wordpress

如何更新自定义post_type自定义字段?是否可以使用 wp_update_post

在这个函数的参数中,我找不到 post_type wp_update_post 是否有正确的功能?感谢您的帮助。

我有自定义帖子类型 crb_expense 。我想更新它的标题,内容,自定义字段和与之相关的分类。在这里,我只测试它是否仅适用于标题和内容:

if ( isset( $_GET['edit'] ) && $_GET['edit'] != 0 ) {
    wp_update_post( [
        'ID'           => $_GET['edit'],
        'post_type'    => 'crb_expense',
        'post_title'   => 'This is the new post title.',
        'post_content' => 'This is the new updated content.',
    ] );
} 

$ _ GET [' edit'] 包含来自ajax请求的帖子的ID。我已经把它弄了,身份证是正确的。

1 个答案:

答案 0 :(得分:1)

wp_update_post 正在根据$ post对象工作,请仔细检查以下代码,了解如何使用自定义字段修改特定的自定义帖子类型。

仅更新帖子标题和内容

 $post_id =1;

  // Update post 1
     $my_post = array(
      'ID'           => $post_id ,
      'post_title'   => 'This is the post title.',
      'post_content' => 'This is the updated content.',
     );

    // Update the post into the database
    wp_update_post( $my_post );

更新帖子

 update_post_meta( $post_id, $meta_key, $meta_value, $prev_value ); 

因此,使用一个功能,您也无法编辑帖子标题,内容和元素。如果您想编辑任何自定义字段,则必须使用 update_post_meta 功能。

这对我有用,我希望它也能为你提供帮助。

相关问题