首页 > 解决方案 > wordpress 帖子上的 post_status 没有改变

问题描述

我有一个代码,我使用我的自定义模板插入帖子,除了 post_status 字段之外,一切都有效,我错过了什么吗?

$new_speakerevent = array(
      'post_ID'      => $page_id,
      'post_title'    => wp_strip_all_tags('HubName'. ' ' .$date. ' ' .'speakerName'),
      'post_content'  => '',
      'post_status'   => 'future',
      'post_type' => 'speakers',
      'post_author'   => 12 //default account is 12
      
     
    );

 $idtoreturn = wp_insert_post( $new_speakerevent );

发生的事情不是未来,而是状态自动更改为 PUBLIC

标签: wordpress

解决方案


计划在未来的某个日期发布。(未来)

即使在 CODEX 中没有逐字指定这一点,future帖子状态也会执行WP-Cron来安排帖子,因此如果post_status设置为futurepost_date需要设置为将来的日期。(因为默认值是插入帖子的当前日期,其null值设置为post_date结果自动回退到post_status => publish)。

add_action( 'init', 'wpso_67525143' );

function wpso_67525143() {

    if ( ! get_page_by_title( 'My Awesome Post Getting Published In The Next +10 days' ) ) {

        $args = array(
            'post_type' => 'page',
            'post_title' => 'My Awesome Post Getting Published In The Next +10 days',
            'post_status' => 'future',
            'post_date' => date( 'c', strtotime( get_the_date( 'Y-m-d' ) . ' + 10 days' ) ),
        );

        wp_insert_post( $args );

    };

};

另一方面,不是,而是wp_strip_all_tags()有一个用于清理标题的功能,称为sanitize_title_with_dashes()


推荐阅读