最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Change scheduled posts to published

programmeradmin3浏览0评论

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.

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.

Share Improve this question edited Mar 19, 2015 at 16:16 Sagive asked Mar 18, 2015 at 23:05 SagiveSagive 2,7822 gold badges35 silver badges54 bronze badges 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 – Pieter Goosen Commented Mar 19, 2015 at 14:12
 |  Show 1 more comment

1 Answer 1

Reset to default 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++;
    }
}

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论