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!