Change scheduled posts to published

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.

1 Answer
1

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++;
    }
}

Leave a Comment