Wordpress 使用wp_insert_post插入帖子ID

Wordpress 使用wp_insert_post插入帖子ID,wordpress,plugins,codex,Wordpress,Plugins,Codex,插入新帖子时,如何选择帖子ID,例如: $post = array( 'ID' => 3333, 'comment_status' => 'open', 'post_content' => 'hi world!', 'post_name' => 'title_1', 'post_status' => 'publish', 'post_title' =&g

插入新帖子时,如何选择帖子ID,例如:

$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);

想插入id=3333的新帖子,对不起,伙计,不可行。以下是开发人员在codex上的发言:

重要信息:为$post['ID']设置值将不会创建具有该ID号的帖子。设置此值将导致函数使用$post中指定的其他值更新具有该ID号的post。简言之,要插入新帖子,$post['ID']必须为空或根本不设置


可以做到这一点,但不能使用API的insert函数。您可以编写自己的插入查询。您总是希望在可以的时候使用API,但有时这是不可能的。查询如下所示:

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
) );

您必须首先确保该ID在数据库中不存在。如果post表架构将来发生更改,您可能需要更新查询以说明更改。

您可能想知道您可以使用
'import\u id'
而不是
'id'
,它将“尝试”并使用它

请参见此处的第二个示例:

,正如daveaspinall所说。 我做了一个这样的函数

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 );
}
例如:

simpleImportPost('My Post 35',35,"35 Content");

以下是我的简单解决方案:

//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\u ID将更新该帖子,而'import\u ID'=>post\u ID将使用该ID创建新帖子


您还可以通过循环和馈送ID来运行多个插入/更新,而无需冒创建无限数量新帖子的风险

id
是数据库中的一个自动递增主字段。我想你做不到。你到底为什么要这样做?我也有一个情况,这是必要的。我正在从另一个站点迁移成千上万的自定义帖子,每个帖子都有分类法和元数据。通过将post ID设置为与旧站点相同,可以更轻松地导入其余数据。正如@daveaspinall所建议的那样,我成功地使用了
import\u id
,这实际上不是正确的答案(尽管被接受)。请参阅此答案,谢谢您告诉我们“导入id”