post_status on wordpress post is not changing

Viewed 127

I have a code on which I insert post using my custom template, everything works aside from post_status field, am I missing something ?

$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 );

What happen is instead of future, the status is automatically changing to PUBLIC

1 Answers

Scheduled to be published in a future date. (future)

Even tho this isn't specified in the CODEX word for word, the future post status perform a WP-Cron to scheduled a post, therefore if post_status is set to future then post_date need to be set to a future date. (As the default value is the current date inserting a post with a null value set to the post_date result in a automatic fallback to 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 );

    };

};

On a side not, instead of wp_strip_all_tags(), there is a function for sanitizing the title called sanitize_title_with_dashes().

Related