将计划发布的帖子更改为已发布

时间:2015-03-18 作者:Sagive

我编写了以下函数,将计划发布的帖子更改为已发布的帖子(我有大约1000篇)。当我运行下面的代码时,日期会更改,但状态不会。。。

有人能解释一下这个问题吗?

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++;
    }
}
我对日期所做的所有“提前一小时”的事情都是可以的,“未来”的帖子实际上当前的日期是昨天,但在帖子列表中仍然有预定状态。

1 个回复
最合适的回答,由SO网友:Sagive 整理而成

好啊因此,当试图将未来的帖子转换为已发布的帖子时,您需要记住设置“post\\u date\\u gmt”,而不仅仅是将“post\\u date”设置为所需的日期。

Thanks to @gmazzap which helped me get there...<下面是一个基于我的问题的工作示例

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

结束

相关推荐