I need to bulk update 86 specific WordPress Pages every 10 minutes, with the term updating I mean the same thing as clicking the Blue Update Button on each page, and having them all update at the same time every 10 minutes. I guess I need to write a function into my theme functions.php file, and maybe trigger the function every 10 minutes with the help of the plugin WP Crontrol, or maybe using the Cron in cPanel. I'm a beginner and can't code php, how can I do this? Thank you.
1 Answer
I will recommend to not use wp cron since will require someone to visit the website. Read more here about cron jobs.
In case you want with wp-cron here is what you need:
Create 10 minutes interval in your schedules if you dont have one.
add_filter( 'cron_schedules', function ( $schedules ) {
$schedules['every_ten_minutes'] = array(
'interval' => 600, // interval is in seconds
'display' => __( 'Ten minutes' )
);
return $schedules;
} );
Create your function. In args you can set your query to your needs more info
function 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');
}
}
And finaly add your cron task
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' );
}
});
-
I've just tried this and I can confirm that it works! I've disabled wp-cron in wp-config.php by adding define('DISABLE_WP_CRON', 'true') so it doesn't get triggered with pageviews, and I'm instead triggering it with a cron job in cpanel that is also set at a 10 minute interval. Every 10 minutes all the requested pages are updated, I can clearly see this in the xml sitemap. Thank you so much for you help!– jhonny4Aug 29, 2021 at 8:19
wp_update_post()
might behave differently than clicking the button. (still you should try it out)