Little-known hack to update all posts or pages in WordPress by code

Sometimes you need to open and update all pages of a WordPress website to get changes to take place.

This might be due to a newly added plugin, or simply a re-organisation of the theme architecture or layers.

If your site has a lot of pages, this can become a hideous task.

With the help of a small function, we can achieve this programmatically, just change the post_type to match what you want to update (post, page or custom post type).

Where do I add this code in WordPress?

Just add this snippet on the function.php of the theme you are using.

If you are unsure on how-to, this is an excellent guide from WP Beginner.

IMPORTANT! Don’t forget to remove the code once the update has been completed!

// Update all post

function update_all_posts() {
    $args = array(
        'post_type' => 'page',
        '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' );

If you need any help with WordPress Development, get in touch!

Mara Milani

Mara Milani

With more than 18 years of commercial experience in digital strategy, Mara is the heart and soul of Marameo Design. Friendly and dedicated, she has a real passion for social causes and fairness. When she is not developing a new digital strategy or theming a website, you can find her on the soccer pitch, cycling around Sydney or at the pub enjoying a pale ale.

LATEST BLOGS

Request a Call Back

Enter your details and we’ll contact you as soon as possible.

Book your "Fast Track" session

 

Request a quote