How to update an ACF field on all of the posts or pages

  • September 15th, 2021
  • 1 minute reading
  • 203 views

Sometime you want to quickly change value of some ACF field on the entire website. In this article you’ll find a quick of how to do that directly in the code rather than update WordPress database page by page in the WP Admin Panel

All you need to do is to go to any PHP file within your theme and paste this code below:

<?php
    $args = array(
        'posts_per_page' => -1,
        'post_type' => 'page',
    );

    $loop = new WP_Query( $args );
    $wp_query = $loop;
?>

<?php if ( $loop->have_posts() ): ?>

	<?php while ( $loop->have_posts() ): $loop->the_post(); ?>

		<?php update_field('slug_of_acf_field', 'new_value'); ?>

	<?php endwhile; ?>

<?php endif; ?>

In the array with arguments for a loop, you can change post_type to any other post type in your theme.

In the update_field function you need to place 2 variables. First, (in our example: slug_of_acf_field put a slug of an ACF field you want to change, and in the second variable (in our example: new_value put the new value of this ACF field.

After saving the file, and refreshing the subpage where you placed this snippet, everything should be updated. If so, you can remove this code from the file and save it again.

Related articles

Comments

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.