Skip to content

Instantly share code, notes, and snippets.

@mattbell87
Last active January 30, 2022 12:07
Show Gist options
  • Select an option

  • Save mattbell87/48c3453c85cc8d34a606937dc828f2ca to your computer and use it in GitHub Desktop.

Select an option

Save mattbell87/48c3453c85cc8d34a606937dc828f2ca to your computer and use it in GitHub Desktop.
Update ACF field for all posts

Here is a quick hack to update an ACF (Advanced Custom Fields) field in WordPress.

Place this in functions.php:

// Update all post
function update_all_posts() {
    $args = array(
        'post_type' => 'event', //or post, page etc
        'numberposts' => -1
    );
    $all_posts = get_posts($args);
    foreach ($all_posts as $single_post){
        //Example
        $value = get_field('event_date', $single_post->ID); 
        update_field('event_date', date("Ymd", $value), $single_post->ID);
    }
}
add_action( 'wp_loaded', 'update_all_posts' );

Now refresh the Wordpress page then delete the code from functions.php (or it will run on every page load).

Credit goes to https://marameodesign.com/insights/little-known-hack-to-update-all-posts-or-pages-in-wordpress-by-code/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment