2

I wrote the following function to change scheduled posts to published (I've got about 1000 of them). When I run the code below, the date change, but the status does not...

Can anyone shed some light on this issue?

This is the code I am using as of now:

/*  CHANGE PENDING POSTS TO PUBLISHED POSTS */ 
function change_post_status($post_id, $status, $change_date){
    $current_post['ID']          = $post_id;
    $current_post['post_status'] = $status;
    $current_post['post_date']   = $change_date;
    wp_update_post($current_post);
}

$args = array(
    'post_type'      => 'post',
    'posts_per_page' => -1,
    'post_status'    => 'future',
    'orderby'        => 'date',
    'order'          => 'ASC',
);
$the_query = new WP_Query($args);

if ($the_query->have_posts()) {
    $counter = 1;
    while ($the_query->have_posts()) {
        $the_query->the_post();
        $pid = get_the_ID();
        $newdate = date('Y-m-d H:i:s', time() - (3600 * $counter));
        change_post_status($pid, 'publish', $newdate);
        // echo 'TITLE: '.get_the_title()
        //      .' TIME:'.date('Y-m-d H:i:s', time()-(3600 * $counter)).'<br/>';
        $counter++;
    }
}

The whole "one hour before" thingy I did with the date is ok - the "future" posts are actually currently with a date of yesterday but still have scheduled status in the posts list.

  • 2
    IIRC WordPress won't set a post published if post_date_gmt is in the future. Try to set it as well. – gmazzap Mar 18 '15 at 23:39
  • ohh?... ok - thanks - didnt knew that - going to try ;) – Sagive SEO Mar 19 '15 at 10:51
  • publish as an answer (so i can mark correct) - adding the line: $current_post['post_date_gmt'] = $change_date; - worked like a charm ;) – Sagive SEO Mar 19 '15 at 10:56
  • 1
    Yes, you should :) @SagiveSEO – gmazzap Mar 19 '15 at 11:45
  • 1
    Yes, you can always mention that gmazzap helped you. :-) Also, questions with accepted answers don't get rotated by the system indefinitely, and it helps others in future – Pieter Goosen Mar 19 '15 at 14:12
2

Ok... so when trying to turn a future post to a published post you need to remember to set "post_date_gmt" and not only the "post_date" to the desired date.

Thanks to @gmazzap which helped me get there...
here is a working example based on my question

/*  CHANGE PENDING POSTS TO PUBLISHED POSTS */ 
function change_post_status($post_id, $status, $change_date){
    $current_post['ID']             = $post_id;
    $current_post['post_status']    = $status;
    $current_post['post_date']      = $change_date;
    $current_post['post_date_gmt']  = $change_date;  // ADDED THIS LINE
    wp_update_post($current_post);
}

$args = array(
    'post_type'      => 'post',
    'posts_per_page' => -1,
    'post_status'    => 'future',
    'orderby'        => 'date',
    'order'          => 'ASC',
);
$the_query = new WP_Query($args);

if ($the_query->have_posts()) {
    $counter = 1;
    while ($the_query->have_posts()) {
        $the_query->the_post();

        $pid        = get_the_ID();
        $newdate    = date('Y-m-d H:i:s', time() - (3600 * $counter));

        change_post_status($pid, 'publish', $newdate);

        // echo 'TITLE: '.get_the_title() .' TIME:'.date('Y-m-d H:i:s', time()-(3600 * $counter)).'<br/>';
        $counter++;
    }
}
  • 1
    Will post_date always be the same as post_date_gmt though? You should probably use get_gmt_from_date($change_date) for that. – Seano Apr 24 '17 at 11:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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