Transition from Draft to Scheduled Post with wp_update_post

Question

I am trying to transition my post from a Draft status to a Scheduled status in the future. Here is my code to do that:

if($save_as_draft == "yes"){
    $post_status = 'draft';
    $post_date = date('Y-m-d H:i:s');
}else{
    $future_date_post = $_POST['future_date_post'];
    if($future_date_post=="yes"){
        echo "it is in here";
        $post_status = 'future';
        $date_time = strtotime($_POST['post_date']);
        $post_date = date('Y-m-d H:i:s', $date_time);
    }else{
        $post_status = 'publish';
        $post_date = date('Y-m-d H:i:s');
    }
}
$bmt_post = array(
    'ID'            => $post_id,
    'post_title'    => wp_strip_all_tags( $title ),
    'post_content'  => $information,
    'post_status'   => $post_status,
    'post_category' => array( $cat_ids ),
    'post_date' => $post_date,
);
print_r($bmt_post);
$result = wp_update_post( $bmt_post );
if($future_date_post=="yes"){
    wp_schedule_single_event( $date_time, 'publish_future_post', array( $result ) );
}

I’m using the exact same code above except for $result = wp_update_post( $bmt_post ); I’m using $post_id = wp_insert_post( $bmt_post ); and when I insert a new post it gives it the Scheduled status and everything works fine.

However, if I’m updating the post from Draft to Future it actually give the status as published with a future date on it. When I echo the array I have the following:

Array ( [ID] => 6944 [post_title] => Test Post With new name [post_content] => Testing this. Additional stuff here. and more stuff. Does this work? [post_status] => future [post_category] => Array ( [0] => 4 ) [post_date] => 2015-01-31 18:29:00 )

So you can see it’s trying to set the [post_status] => future but then something happens and it’s changed to published.

According to the codex it seems like this should work:
http://codex.wordpress.org/Function_Reference/wp_transition_post_status

Then the codex says:
To transition the status of a post, rather than perform actions when a post status is transitioned, use wp_update_post() or wp_publish_post().

Any idea why this works for creating a new post but not updating a new post?

0
user1048676 3 years 2020-04-01T12:52:45-05:00 0 Answers 88 views 0

Leave an answer

Browse
Browse