如何在一段时间后改变wordpress帖子的状态?

时间:2018-06-04 02:18:52

标签: wordpress woocommerce hook-woocommerce

我有一个wordpress网站,用户可以上传产品。默认产品状态是待处理的,但是如果在post_time之后12小时已经过了,我想将其更改为已发布。

我正在使用woocommerce。 我曾尝试使用 wp_insert_post_data

function reset_post_date_wpse_121565($data,$postarr) {
    if ($data['post_type'] == 'product'){
        $data['post_date'] = '2018-06-03 22:50:00';
    } 
    return $data;
}
add_filter('wp_insert_post_data','reset_post_date_wpse_121565');

但它将帖子设置为已发布,我该怎么做才能完成此自动更新?

1 个答案:

答案 0 :(得分:0)

您可以尝试运行WordPress Cron来实现这一目标,

function myprefix_custom_cron_schedule( $schedules ) {


 $schedules['every_tewlve_hours'] = array(
        'interval' => 43200, // Every 12 hours
        'display'  => __( 'Every 12 hours' ),
    );
    return $schedules;
}
add_filter( 'cron_schedules', 'myprefix_custom_cron_schedule' );

//Schedule an action if it's not already scheduled
if ( ! wp_next_scheduled( 'myprefix_cron_hook' ) ) {
    wp_schedule_event( time(), 'every_tewlve_hours', 'myprefix_cron_hook' );
}

///Hook into that action that'll fire every six hours
 add_action( 'myprefix_cron_hook', 'myprefix_cron_function' );

//create your function, that runs on cron
function myprefix_cron_function() {
    $args = array(
    'numberposts'   => -1,
    'post_type'     => 'product',
    'post_status'   => array( 'pending' ),
    'author'        => $userid
   );
   $pending_posts = get_posts( $args ); 
//update status code
}

希望这会对你有所帮助。

有关更多示例,请访问

wp cron demo