我正在尝试以编程方式创建自定义帖子类型并将其从界面更新到 Wordpress。 我有以下代码,除了已发布/计划状态之外,一切正常:
wp_update_post(
array(
'ID' => $id,
'post_title' => $page_title,
'post_excerpt' => $page_excerpt,
'post_content' => $page_content,
'edit_date' => true,
'post_date' => $publish_date,
'post_status' => ( strtotime( $publish_date ) > time( ) ? 'future' : 'publish' )
)
);
当使用 wp_insert_post 创建帖子时,这似乎正确设置了状态(使用与上面相同的逻辑),但是当更新日期(并相应地设置状态)时,状态永远不会改变。
例如如果帖子是在未来 6 个月后创建的,则它会被设置为预定的。如果该帖子的 post_date 已过去,则日期会更新,但状态仍设置为已安排。
我读到您需要将 edit_date 设置为 true 才能正常工作,但我已经按照上面的方法进行了尝试,但仍然没有成功。
我有什么遗漏吗?
预先感谢您的帮助。
问候,
菲尔半
最佳答案
您可以尝试一下(已添加post_date_gmt
)
$status = strtotime($publish_date) > strtotime('today') ? 'future' : 'publish';
wp_update_post(
array(
'ID' => $id,
'post_title' => $page_title,
'post_excerpt' => $page_excerpt,
'post_content' => $page_content,
'edit_date' => true,
'post_date' => $publish_date,
'post_status' => $status,
'post_date_gmt' => gmdate( 'Y-m-d H:i:s', strtotime($publish_date) )
)
);
看看this answer .
关于WordPress wp_update_post 未正确更新状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20459452/