0

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?

1 Answer 1

2

Try adding edit_date => true to your bmt_post array, like so:

$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,
    'edit_date' => true
);

If you look at the function wp_update_post found here: scheduling posts with wp_update_post you can see that scheduling won't work without this parameter being set

Hopefully this solves your issue?

6
  • Thanks for the help. I totally forgot that I read that somewhere. For some reason, it's still doing the same thing and setting it as published instead of scheduled. Commented Jan 29, 2015 at 19:10
  • How are you calling this whole function? Is it called on the save_post() action? Commented Jan 29, 2015 at 19:43
  • I'm submitting a form to it and then processing the form and trying to update the post. Commented Jan 29, 2015 at 20:08
  • The form is being submitted to the same page so I'm not tying into any hooks or actions, just calling the wp_update_post directly. Commented Jan 29, 2015 at 20:21
  • 2
    I actually needed to add post_date_gmt and now it started working. See this answer: wordpress.stackexchange.com/questions/67451/… Commented Jan 29, 2015 at 21:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.