I’m using this bit to insert/update a custom post type from the front-end. The date is set from a custom jquery datepicker.

    if (strtotime($date) < strtotime('tomorrow')) {
            $newpostdata['post_status'] = 'publish';
    } elseif (strtotime($date) > strtotime('today')) {
            $newpostdata['post_status'] = 'future';
            $newpostdata['post_date'] = date('Y-m-d H:i:s', strtotime($date));
    }

    if ('insert' == $operation) {
        $err = wp_insert_post($newpostdata, true);
    } elseif ('edit' == $operation) {
        $newpostdata['ID'] = $post_id;
        $err = wp_update_post($newpostdata);
    }

This works when first publishing the post, setting it correctly as publish or future, with the correct date.

This doesn’t work when editing the same post, neither from publish to future nor the other way around. Everything else is properly updated, except for the post status/future_date.

UPDATE Oct. 9th
I’m starting to think this might be a bug, so i started a conversation at the wp-hackers mailing list, about this. There’s some fresh install test results on the link.


OTHER FAILED ATTEMPTS:

i’ve tried leaving post_status decisions to wp_insert_post() using:

elseif ('edit' == $operation) {
        $newpostdata['post_status'] = '';
        $newpostdata['ID'] = $post_id;
        $err = wp_update_post($newpostdata);
    }

And this sets the post status to draft while maintaning the requested dates.

I’ve also tried calling wp_transition_post_status() again (it’s called once inside wp_insert_post()):

elseif ('edit' == $operation) {
        $newpostdata['ID'] = $post_id;
        $err = wp_update_post($newpostdata);
        wp_transition_post_status($old_status, $status, $post_id);
    }

but that also didn’t seem to work.

I’m running out of ideas here. Any clues?

6 s
6

couldn’t be simpler.

As pointed out by Otto at the wp-hackers list, problem was me not setting post_date_gmt when using wp_update_post().

Final code looks like this:

if ( $post_date < strtotime( "tomorrow" ) ) {
        $status="publish";    
        $newpostdata['post_status'] = $status;
        $newpostdata['post_date'] = date( 'Y-m-d H:i:s',  $post_date );

        // Also pass 'post_date_gmt' so that WP plays nice with dates
        $newpostdata['post_date_gmt'] = gmdate( 'Y-m-d H:i:s', $post_date );

    } elseif ( $post_date > strtotime( 'today' ) ) {
        $status="future";    
        $newpostdata['post_status'] = $status;
        $newpostdata['post_date'] = date( 'Y-m-d H:i:s', $post_date );

        // Also pass 'post_date_gmt' so that WP plays nice with dates
        $newpostdata['post_date_gmt'] = gmdate( 'Y-m-d H:i:s', $post_date );
    }

    if ('insert' == $operation) {
        $err = wp_insert_post($newpostdata, true);
    } elseif ('edit' == $operation) {
        $newpostdata['ID'] = $post_id;
        $err = wp_update_post($newpostdata);
    }

Leave a Reply

Your email address will not be published. Required fields are marked *