Insert message id with wp_insert_post

how can I select a message id when inserting a new message, for example:

$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 a new message with id = 3333

+5
source share
5 answers

Sorry, buddy, not doable. Here's what the developers say in the code:

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

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

+5
source

Thought it might seem to you that you can use 'import_id' instead of 'ID' , and he will โ€œtryโ€ and use it.

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

+17
source

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 this message, and 'import_id' => post_id will create a new message with this identifier.

You can also view and pass identifiers to run multiple inserts / updates without the risk of creating an infinite number of new messages.

+3
source

This can be done, just not using the API insert function. Instead, you can write your own INSERT query. You always want to use the API whenever you can, but sometimes itโ€™s not possible. The request will look like this:

 global $wpdb; $wpdb->query( $wpdb->prepare(" INSERT INTO {$wpdb->posts} VALUES( %d, %d, NOW(), %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, NOW(), %s, %s, %d, %s, %d, %s, %s, %d )", $ID, $post_author, $post_date_gmt, $post_content, $post_title, $post_excerpt, $post_status, $comment_status, $ping_status, $post_password, $post_name, $to_ping, $pinged, $post_modified_gmt, $post_content_filtered, $post_parent, $guid, $menu_order, $post_type, $post_mime_type, $comment_count ) ); 

You need to make sure that the identifier does not already exist in the database. If the mail table layout changes in the future, you may need to update your account request for changes.

0
source

As they say daveaspinall. I perform a function that does this.

 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"); 
0
source

All Articles