在使用wp_insert_post函数时如何插入文章ID?

11

当我插入新文章时,如何选择要使用的文章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的新文章


id 是数据库中的自增主键字段。我不认为你能做到这一点。你为什么要这样做呢? - Dogbert
1
我也有这种情况。我正在从另一个站点迁移数千个自定义文章,每个文章都有分类和元数据。通过将文章ID设置为与旧站点相同,可以更轻松地导入其余数据。我成功地使用了import_id,正如@daveaspinall下面建议的那样。 - Astrotim
对于未来的读者,请向下滚动至已接受答案下方,因为下面有一个非常有用的答案。 - armadadrive
5个回答

27

它还可以与wp_insert_attachment一起使用。 - undefined

10

这是我的简单解决方案:

//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 将更新该文章,而 'import_id'=> post_id 将创建一个具有该 ID 的新文章。

您还可以循环遍历并将 ID 提供给运行多个插入/更新,而不会创建无限数量的新文章。


1
感谢您提到“import_id”。 - Farzad Hoseinzade

2
抱歉伙计,无法完成。以下是开发人员在代码库中的声明:
重要提示:设置$post['ID']的值不会创建具有该ID编号的文章。设置此值将导致该函数使用$post中指定的其他值更新该ID编号的文章。简而言之,要插入新文章,$post['ID']必须为空或根本未设置。

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


5
这并不是正确答案(尽管被接受了)。请看这个答案代替:https://dev59.com/c2w15IYBdhLWcg3whMK_#8153962 - Ben

1

这是可能的,只是不能使用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在数据库中不存在。如果将来发布表模式更改,可能需要更新查询以适应这些更改。


1
作为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");

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接