insert post ID with wp_insert_post

15,432

Solution 1

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

Solution 2

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

Solution 3

Here is my simple solution:

//check if post with id 3333 is already in database, if so, update post 3333
if (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 3333
else {
    $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.

Solution 4

As daveaspinall say. I do a function that do that.

require( 'wp-load.php' );
function simpleImportPost($title,$import_id,$content){
// Create post object
$my_post = array();
$my_post['post_title'] = $title;
$my_post['import_id']=$import_id;
$mypost['comment_status'] = 'closed';//I'll set all closed
$my_post['post_content'] = $content;
$my_post['post_status'] = 'publish';
$my_post['post_author'] = 1;
$my_post['post_category'] = array(0);
// Insert the post into the database
return wp_insert_post( $my_post );
}

example:

simpleImportPost('My Post 35',35,"35 Content");
Share:
15,432
mwafi
Author by

mwafi

PHP Developer at Grow knowledge www.wokcraft.com

Updated on July 27, 2022

Comments

  • mwafi
    mwafi almost 2 years

    how can I choose post ID, when inserting new post, ex:

    $post = array(
    'ID'                =>  3333,
    'comment_status'            =>  'open',
    'post_content'      =>  'hi world!',
    'post_name'         =>  'title_1',
    'post_status'       =>  'publish',
    'post_title'        =>  'sdfsfd fdsfds ds',
    'post_type'         =>  'post',
    );  
    
    $post_id = wp_insert_post($post);
    

    want to insert new post with id = 3333

    • Dogbert
      Dogbert almost 13 years
      id is an auto increment primary field in the database. I don't suppose you can do that. Why exactly do you want to do this?
    • Astrotim
      Astrotim almost 11 years
      I also have a situation where this is necessary. I am migrating thousands of custom posts from another site, each with taxonomy and meta data. By setting the post ID to the same as the old site, it makes it much easier to import the rest of the data. I was able to use import_id successfully, as suggested by @daveaspinall below
    • armadadrive
      armadadrive over 2 years
      For future readers, please scroll below the accepted answer as there is a very useful answer below.
  • Ben
    Ben about 7 years
    This is not actually the correct answer (despite being accepted). See this answer instead stackoverflow.com/a/8153962/192705
  • Farzad Hoseinzade
    Farzad Hoseinzade almost 4 years
    Thanks for telling about "import_id"