【问题标题】:Wordpress wp_update_post not updating status correctlyWordpress wp_update_post 未正确更新状态
【发布时间】:2013-12-09 06:02:08
【问题描述】:

我正在尝试以编程方式创建自定义帖子类型并将其从界面更新到 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 才能使其正常工作,但我已经按照上述方法进行了尝试,但仍然没有成功。

我有什么遗漏吗?

提前感谢您的帮助。

问候,

菲尔半

【问题讨论】:

    标签: wordpress


    【解决方案1】:

    你可以试一试(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

    【讨论】:

    • 欢迎@PhilHalf。很高兴它有帮助:-)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-10-15
    • 2021-12-14
    • 1970-01-01
    • 1970-01-01
    • 2015-03-26
    • 2021-12-01
    • 2020-01-18
    相关资源
    最近更新 更多