admin管理员组

文章数量:1323330

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:

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?

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/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?

Share Improve this question asked Jan 29, 2015 at 18:39 user1048676user1048676 4373 gold badges9 silver badges25 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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?

本文标签: Transition from Draft to Scheduled Post with wpupdatepost