mwa*_*afi 4 wordpress plugins codex
如何在插入新帖子时选择帖子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);
Run Code Online (Sandbox Code Playgroud)
想要插入id = 3333的新帖子
dav*_*all 16
以为你可能想知道你可以使用'import_id'而不是'ID'它会"尝试"并使用它.
请参阅此处的第二个示例:http://codex.wordpress.org/Function_Reference/wp_insert_post#Example
小智 8
这是我的简单解决方案:
//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);
}
Run Code Online (Sandbox Code Playgroud)
'ID'=> post_id 将更新该帖子,而 'import_id'=> post_id 将使用该 ID 创建一个新帖子。
您还可以循环并提供 ID 以运行多个插入/更新,而无需创建无限数量的新帖子。
对不起哥们,不可行.以下是开发人员在代码中所说的内容:
重要提示:为$ post ['ID']设置值不会创建具有该ID号的帖子.设置此值将导致该函数使用该ID号更新帖子,并使用$ post中指定的其他值.简而言之,要插入新帖子,$ post ['ID']必须为空或根本不设置.
http://codex.wordpress.org/Function_Reference/wp_insert_post