0

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.

3
  • 1
    X/Y problem maybe - why do you need to click that update button every 10 minutes?
    – kero
    Aug 26, 2021 at 9:45
  • I want to enable CloudFlare APO, it works OK for 90% of my website, but I have 86 pages with WP Job Manager Classified Listings that reorder themselves randomly at each page refresh from the browser. I'm using WP Litespeed Cache and I set a TTL of 10 minutes for these 86 pages, so site visitors see the listings randomly reordered every 10 minutes, and this is OK for my needs. The problem is that APO only caches a new version of the page when the page update button is pressed, or I can use a page rule in CloudFlare to lower the Edge Cache TTL but the lowest value is 1 hour which is too much.
    – jhonny4
    Aug 26, 2021 at 23:37
  • So actually, you want to flush APO's cache all 10mins for these pages, right? I know too little about APO on how to do it, sorry. But it's possible the posted answer doesn't work because wp_update_post() might behave differently than clicking the button. (still you should try it out)
    – kero
    Aug 27, 2021 at 11:34

1 Answer 1

0

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' );
    }
});
1
  • 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!
    – jhonny4
    Aug 29, 2021 at 8:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.