我建议不要使用wp cron,因为它需要有人访问该网站。阅读更多信息here 关于cron作业。
如果您想使用wp cron,您需要:
如果你没有10分钟的时间间隔,那么在你的时间表中创建10分钟的时间间隔。
add_filter( \'cron_schedules\', function ( $schedules ) {
$schedules[\'every_ten_minutes\'] = array(
\'interval\' => 600, // interval is in seconds
\'display\' => __( \'Ten minutes\' )
);
return $schedules;
} );
创建您的函数。在args中,您可以根据需要设置查询
more infofunction update_all_selected_posts() {
$args = array(
\'post_type\' => \'post\', // select your proper post type
\'numberposts\' => -1 // get all posts if they are 86 in total
//use either custom meta to select your posts or post__in and array your ids.
);
$all_posts = get_posts($args);
//Loop through all posts and update
foreach ($all_posts as $single_post){
wp_update_post( $single_post );
$time = current_time(\'d/m/Y H:i\');
//Enable your wp debug in config and check your error_log how its working
error_log($time.\': Post with id \'.$single_post->ID.\' is updated\');
}
}
最后添加cron任务
add_action(\'init\', function() {
add_action( \'update_all_selected_posts_cron\', \'update_all_selected_posts\' );
if (! wp_next_scheduled ( \'update_all_selected_posts_cron\' )) {
wp_schedule_event( time(), \'every_ten_minutes\', \'update_all_selected_posts_cron\' );
}
});