我需要每10分钟批量更新86个特定的WordPress页面,更新一词的意思与单击每个页面上的蓝色更新按钮相同,并让它们每10分钟同时更新一次。我想我需要在我的主题函数中写一个函数。php文件,并且可能在插件WP Crontrol的帮助下每10分钟触发一次函数,或者可能在cPanel中使用Cron。我是个初学者,不会编写php代码,我该怎么做?非常感谢。
每10分钟批量更新一组WordPress页面
1 个回复
最合适的回答,由SO网友:Martin Mirchev 整理而成
我建议不要使用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\' );
}
});