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.

6
  • 2
    IIRC WordPress won't set a post published if post_date_gmt is in the future. Try to set it as well.
    – gmazzap
    Commented Mar 18, 2015 at 23:39
  • ohh?... ok - thanks - didnt knew that - going to try ;)
    – Sagive
    Commented Mar 19, 2015 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
    Commented Mar 19, 2015 at 10:56
  • 1
    Yes, you should :) @SagiveSEO
    – gmazzap
    Commented Mar 19, 2015 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 Commented Mar 19, 2015 at 14:12

1 Answer 1

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
  • 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
    Commented Apr 24, 2017 at 11:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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