WordPress wp_update_post not updating status correctly

I am trying to programatically create and update a custom post type from an interface into WordPress.
I have the following code and everything works, except for the published / scheduled status:

wp_update_post(
    array(
        'ID' => $id,
        'post_title' => $page_title,
        'post_excerpt' => $page_excerpt,
        'post_content' => $page_content,
        'edit_date' => true,
        'post_date' => $publish_date,
        'post_status' => ( strtotime( $publish_date ) > time( ) ? 'future' : 'publish' )
    )
);

When creating the post with wp_insert_post, this seems to set the status correctly (using the same logic as above), but when updating the date (and setting the status accordingly), the status never changes.

Read More

e.g. if a post is created 6 months into the future, it is set to scheduled. If that post is updated with a post_date in the past, the date is updated, but the status is still set to scheduled.

I’ve read that you need to set edit_date to true for it to work, but I’ve tried that as above and still no luck.

Is there something I’m missing?

Thanks in advance for any help.

Regards,

PhilHalf

Related posts

Leave a Reply

1 comment

  1. You may give it a try (post_date_gmt added)

    $status = strtotime($publish_date) > strtotime('today') ? 'future' : 'publish';
    wp_update_post(
        array(
            'ID' => $id,
            'post_title' => $page_title,
            'post_excerpt' => $page_excerpt,
            'post_content' => $page_content,
            'edit_date' => true,
            'post_date' => $publish_date,
            'post_status' => $status,
            'post_date_gmt' => gmdate( 'Y-m-d H:i:s', strtotime($publish_date) )
        )
    );
    

    Take a look at this answer.