在我的wordpress插件中,我有以下代码:
$args = array(
'post_status' => 'draft',
'category' => 1,
'post_type' => array('page', 'post', 'attachment' ),
'posts_per_page' => -1,
);
$allposts = get_posts($args);
foreach ( $allposts as $key => $post ) {
$update = [];
$update['ID'] = $post->ID;
$update['post_title'] = 'post title';
$update['post_name'] = 'post-name';
$post_id = wp_update_post( $update , true);
//fail, loop stopped without any errors
var_dump($post_id); //never happens
}
$allposts
包含有效的帖子对象数组
第一次迭代更新第一篇文章,但wp_update_post
绝对没有返回任何内容,没有错误,没有帖子ID,没有
所以下一次迭代永远不会开始...
该代码以前完美无缺,但其中一个时刻已经破解
这里发生了什么?
P.S。在其他博客上,这段代码仍然完美无缺
答案 0 :(得分:0)
在我的系统中检查下面的代码,发布帖子状态。
add_action ('init',array( $this, 'updateAllPost'));
public function updateAllPost()
{
$args = array(
'post_status' => 'draft',
'category' => 1,
'post_type' => array('page', 'post', 'attachment' ),
'posts_per_page' => -1,
);
$allposts = get_posts($args);
foreach ( $allposts as $key => $post ) {
$update = array(
"ID"=>$post->ID,
"post_title"=>"post title",
"post_name"=>"post-name"
);
$post_id = wp_update_post( $update , true);
//fail, loop stopped without any errors
var_dump($post_id); //never happens
}
}