'After syncing ACF fields, ACF data is not reachable on REST API / Export. I have to manually save the post again to make it show, how can I mass save?

I have a large database with hundreds of posts. At some point, ACF became out of sync and the client has been adding in content to multiple custom fields on the blog content type.

Say for example, we have a checkbox called 'Display CTA Box'. If I go to a post, it is checked. If I go to the WordPress REST Api or export the post, 'Display CTA Box' is false.

If I go to the post again and manually click 'Update'. The backend updates and it now displays as true.

Instead of me having to manually do this for hundreds of posts, is there a better way to do it?

What I've tried:

  1. Bulk selecting posts, going to 'Edit', adding a category, clicking 'Update'

  2. The below function:

     function update_all_posts() {
     $args = array(
         'post_type' => 'post',
         'post_status' => 'publish',
         'posts_per_page' => -1,
         'orderby' => 'date',
         'order' => 'DESC',
     );
    
    
     $posts = get_posts( $args );
     foreach ( $posts as $post ) {
         wp_update_post( $post );
     }
    

    }

    add_action( 'init', 'update_all_posts', 20 );

The ONLY way I can make it work, is again, going to the post, opening it fully and clicking 'Update'.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source