Programmatically Force Update all Posts

Posted on Dec 01, 2025

by Jimmy


Handy snippet that will force refresh all posts on save. Useful after adding an ACF field and needing to update every post.

This is useful when:

  • You added a new ACF field
  • You changed a field key
  • You need ACF’s update_field() or hooks to run again
  • You want to trigger custom code on save_post

How it works

Temporarily add the code below to the functions.php file. Refresh the page and then remove the code. Your posts should now all work as expected.

PHP
        
            <?php
add_action('init', function () {

    // Run once only
    if (get_option('force_resave_done')) {
        return;
    }

    $post_types = ['post', 'page', 'project']; // Add any other custom types

    foreach ($post_types as $post_type) {
        $posts = get_posts([
            'post_type' => $post_type,
            'posts_per_page' => -1,
            'post_status' => 'any',
            'fields' => 'ids',
        ]);

        foreach ($posts as $post_id) {
            // Load fields into cache
            acf_get_fields($post_id);

            // Trigger WordPress + ACF save hooks
            wp_update_post([
                'ID' => $post_id,
            ]);
        }
    }

    update_option('force_resave_done', true); // Prevent running twice
});
        
    

Back to Snippets