0

How combine wp_insert_post() and wp update_post_meta?

I need first insert my Post then, update the Post. Let me know about an example? I want a sample for my future projects.

0

1 Answer 1

4

2020 Update:

In case you landed here looking for a refresher on how to update the post's meta, as I did, see the following edit:

The post array argument in wp_insert_post() takes an inner array called meta_input, so you can create the post with proper meta in one database query.

$page_id = wp_insert_post(array(
    'post_title'    =>  'Team',
    'menu_order'    =>  '2',
    'post_content'  =>  '[team]',
    'post_type'     =>  'page',
    'post_parent'   =>  1,
    'post_status'   =>  'publish',
    'meta_input'    =>  array(
        'your_meta_key' => 'your meta value'
    ),
));

The same can be done for wp_update_post():

wp_update_post(array(
  'post_title' => 'About',
  'ID' => $page_id,
  'meta_input' =>  array(
    'your_meta_key' => 'your meta value'
  ),
));

Original Answer:

is this code enough for you?

/*
** wp_insert_post - http://codex.wordpress.org/Function_Reference/wp_insert_post
** function will create a new post and return a $interger shich is a ID of the new created post.
*/
$page_id = wp_insert_post(array(
    'post_title'    =>  'Team',
    'menu_order'    =>  '2',
    'post_content'  =>  '[team]',
    'post_type'     =>  'page',
    'post_parent'   =>  1,
    'post_status'   =>  'publish',
));

/*
**  wp_update_post - http://codex.wordpress.org/Function_Reference/wp_update_post
** function is simular (actually its almost alias) of wp_insert_post
** it will update a post 
** $arguments that passed to wp_update_post is a same as in wp_insert_post but require additional argument ID whish is id of the post
*/
wp_update_post(array('post_title' => 'About', 'ID' => $page_id));
5
  • thanks this corrected? $my_post = wp_insert_post (array( 'post_title' => $ff[0], 'post_status' => 'publish', 'post_author' => 1, 'post_type' => 'products', )); $new_p = wp_insert_post( $my_post ); wp_set_post_terms($new_p,$term_s_child->term_id,'phone'); update_post_meta($new_p, 'price', $ff[1]);
    – John Genry
    Oct 17, 2013 at 15:20
  • NO. In your example you creating post (first instance of wp_insert_post with some arguments) - than you tring to create a new post with invalid argument (which is ID of previously creacted post)... next code line skipped. take a look to code reference - codex.wordpress.org/Function_Reference/wp_insert_post Oct 17, 2013 at 15:25
  • as will be right then? I need after the creation of the post, update it
    – John Genry
    Oct 17, 2013 at 15:50
  • so use wp_update_post (same arguments + post id (ID)) - see codex.wordpress.org/Function_Reference/wp_update_post Oct 17, 2013 at 15:58
  • Oleg, please add explanations to your answer, not as a comment. Thanks.
    – kaiser
    Oct 22, 2013 at 11:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.