insert post ID with wp_insert_post insert post ID with wp_insert_post wordpress wordpress

insert post ID with wp_insert_post


Thought you might like to know you can use 'import_id' instead of 'ID' and it will "try" and use that.

See the second example here: http://codex.wordpress.org/Function_Reference/wp_insert_post#Example


Here is my simple solution:

//check if post with id 3333 is already in database, if so, update post 3333if (get_post_status(3333) ) {    $post = array(    'ID'                =>  3333,    'comment_status'    =>  'open',    'post_content'      =>  'hi world!',    'post_name'         =>  'title_1',    'post_status'       =>  'publish',    'post_title'        =>  'your title',    'post_type'         =>  'post',    );      $post_id = wp_insert_post($post);}//if not in database, add post with id 3333else {    $post = array(    'import_id'         =>  3333,    'comment_status'    =>  'open',    'post_content'      =>  'hi world!',    'post_name'         =>  'title_1',    'post_status'       =>  'publish',    'post_title'        =>  'your title',    'post_type'         =>  'post',    );      $post_id = wp_insert_post($post);}

'ID'=> post_id will update that post, while 'import_id'=> post_id will create a new post with that id.

You can also loop through and feed the IDs to run multiple insertions/updates without the risk of creating an infinite amount of new posts.


Sorry buddy, not doable. Here is what the devs say at the codex:

IMPORTANT: Setting a value for $post['ID'] WILL NOT create a post with that ID number. Setting this value will cause the function to update the post with that ID number with the other values specified in $post. In short, to insert a new post, $post['ID'] must be blank or not set at all.

http://codex.wordpress.org/Function_Reference/wp_insert_post