How can I mass-update/save all WordPress posts and pages?

The question:

I’m using a free SEO plugin for WordPress called Rank Math – https://wordpress.org/plugins/seo-by-rank-math/

Overall, I like it a lot. But it has some problems for users with existing WP sites who are importing data from another plugin like Yoast.

Rank Math is relying on individual posts and pages to be edited (not using WP’s bulk quick edit feature, but opening each post and re-saving them one-by-one).

This happens with author sitemaps for example. An author’s page is only added to the sitemap if I manually open and re-save one of their posts and then re-save the sitemap settings. It doesn’t find and add existing authors otherwise.

Is there any way to simulate clicking the “update” button for all posts and pages without changing any actual content in a circumstance like this?

Thanks.

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

Add to your functions.php file, but dont forget to take it off after use it !!!!!

function update_all_posts() {
    $args = array(
        'post_type' => 'post',
        'numberposts' => -1
    );
    $all_posts = get_posts($args);
    foreach ($all_posts as $single_post){
        $single_post->post_title = $single_post->post_title.'';
        wp_update_post( $single_post );
    }
}
add_action( 'wp_loaded', 'update_all_posts' );

PS : BE SURE TO MAKE A DATABASE BACKUP BEFORE EXECUTE !!!


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment