我正在使用此位从前端插入/更新自定义帖子类型。日期是从自定义jquery日期选择器设置的。
if (strtotime($date) < strtotime(\'tomorrow\')) {
$newpostdata[\'post_status\'] = \'publish\';
} elseif (strtotime($date) > strtotime(\'today\')) {
$newpostdata[\'post_status\'] = \'future\';
$newpostdata[\'post_date\'] = date(\'Y-m-d H:i:s\', strtotime($date));
}
if (\'insert\' == $operation) {
$err = wp_insert_post($newpostdata, true);
} elseif (\'edit\' == $operation) {
$newpostdata[\'ID\'] = $post_id;
$err = wp_update_post($newpostdata);
}
This works 首次发布帖子时,请将其正确设置为
publish
或
future
, 有正确的日期。
This doesn\'t work 编辑同一篇文章时,无论是从publish
到future
反之亦然。其他所有内容都已正确更新,但post状态/future\\u日期除外。
UPDATE Oct. 9th 我开始觉得这可能是个bug,so i started a conversation at the wp-hackers mailing list, 关于这个。链接上有一些新的安装测试结果。
<小时>
OTHER FAILED ATTEMPTS:
我试着离开
post_status
决定
wp_insert_post()
使用:
elseif (\'edit\' == $operation) {
$newpostdata[\'post_status\'] = \'\';
$newpostdata[\'ID\'] = $post_id;
$err = wp_update_post($newpostdata);
}
这会将post状态设置为
draft
在维护请求的日期时。
我也试过打电话wp_transition_post_status()
再次(在wp\\u insert\\u post()中调用一次):
elseif (\'edit\' == $operation) {
$newpostdata[\'ID\'] = $post_id;
$err = wp_update_post($newpostdata);
wp_transition_post_status($old_status, $status, $post_id);
}
但这似乎也不起作用。
我的想法快用完了。有什么线索吗?
最合适的回答,由SO网友:moraleida 整理而成
答案再简单不过了。
正如Otto 在wp黑客列表中,问题是我没有设置post_date_gmt
使用时wp_update_post()
.
最终代码如下所示:
if ( $post_date < strtotime( "tomorrow" ) ) {
$status = \'publish\';
$newpostdata[\'post_status\'] = $status;
$newpostdata[\'post_date\'] = date( \'Y-m-d H:i:s\', $post_date );
// Also pass \'post_date_gmt\' so that WP plays nice with dates
$newpostdata[\'post_date_gmt\'] = gmdate( \'Y-m-d H:i:s\', $post_date );
} elseif ( $post_date > strtotime( \'today\' ) ) {
$status = \'future\';
$newpostdata[\'post_status\'] = $status;
$newpostdata[\'post_date\'] = date( \'Y-m-d H:i:s\', $post_date );
// Also pass \'post_date_gmt\' so that WP plays nice with dates
$newpostdata[\'post_date_gmt\'] = gmdate( \'Y-m-d H:i:s\', $post_date );
}
if (\'insert\' == $operation) {
$err = wp_insert_post($newpostdata, true);
} elseif (\'edit\' == $operation) {
$newpostdata[\'ID\'] = $post_id;
$err = wp_update_post($newpostdata);
}