Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I schedule posts in a wp plugin (WP 3.8) with the following function. My posts seem to be in the correct status after scheduling them (when editing them, date time and status are good), and also after a database check with phpmyadmin, where the record in the post table seems coherent with another record corresponding to a post scheduled with wp classic edit interface. But the post is never published, and stays in the scheduled status. Second problem, the date retrieve by strtotime('now') is one hour late (here is why i do 'strtotime('+1 hour')') repecting to the local time (displayed on my server with 'date' command) Can you help please ?

function updatePostDate($blog_id, $post_id, $post_date) {
    $status = strtotime($post_date) > strtotime('+1 hour') ? 'future' : 'publish';
    switch_to_blog( $blog_id );
    $operation = 'edit';
    $newpostdata = array();
    if ( $status == 'publish' ) {
        echo ' ' .strtotime($post_date) .'('.$post_date. ') < '. strtotime( "now" ) ,"\n" ;
        echo 'cannot publish artilces from here, only schedule, dates in future';
        return;
    } elseif ( $status == 'future' ) {
        echo '<br/>SCHEDULING: ' .strtotime($post_date) . '>'. strtotime( "today" ) .'\r\n';
        $newpostdata['post_status'] = $status;
        $newpostdata['post_date'] = date( 'Y-m-d H:i:s', strtotime($post_date) );
        $newpostdata->edit_date = true;
         // Also pass 'post_date_gmt' so that WP plays nice with dates
        $newpostdata['post_date_gmt'] = gmdate( 'Y-m-d H:i:s', strtotime($post_date) );
        echo '<br/>SCHEDULING: ' . $newpostdata['post_date'] . ' / GMDate : ' . $newpostdata['post_date_gmt'];
    }

    if ('insert' == $operation) {
        $err = wp_insert_post($newpostdata, true);
    } elseif ('edit' == $operation) {
        $newpostdata['ID'] = $post_id;
        $err = wp_update_post($newpostdata);
  //echo "wp_update_post::Error return: ".$err ."\r\n";
    }
}

Thank you very much!

share|improve this question

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.