Handy snippet that will force refresh all posts on save. Useful after adding an ACF field and needing to update every post.
update_field() or hooks to run againsave_postTemporarily 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
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
});