1
votes

How to update the custom post_type custom fields? Is it possible using wp_update_post ?

In this function's arguments I couldn't find post_type. Is wp_update_post the right function for the purpose? Thanks for the help in advance.

I have custom post type crb_expense. I would like to update it's title, content, custom fields and taxonomy related with it. Here I just test if it works only with title and the content:

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'] contains the id of the post which is comming from ajax request. I have cheched it and the id is correct.

1
where are you using wp_update_post? be more detailed - Samvel Aleqsanyan
ok, add your comment into your question( edit it ) - Samvel Aleqsanyan
you have to use the following for custom fields update_post_meta($id,'fieldkey', $_REQUEST['fieldvalue']); - Vignesh Pichamani

1 Answers

3
votes

wp_update_post is working based on $post object so kindly check below code how you can edit particular custom post type with custom fields.

Update post title and content only

 $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

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

So using one function you can't edit post title, content and meta as well. Of you want to edit any custom fields then you have to use update_post_meta function.

This is working for me I hope it will help you as well.