Update ACF field on all posts programmatically

The script below fetches all posts of type “tribe_events”.
Then, it goes through the post instances and updates the ACF field. You need to be careful with it as you can override some fields. The script needs to run only once. Back-up the db before.

When creating a new ACF field all the posts need to be updated. If it’s not done, then any queries that rely on the new fields will not work properly, even if the field’s default value is set to null. The solution is to go through all the posts and update them manually. It’s manageable for small websites, but for bigger ones, the following script will help.

This technique can also be used when migrating or populating the website with API data.

$args = array(
	'post_type' => 'tribe_events',
	'nopaging' => true,
);

$the_query = new WP_Query( $args );
 
if ( $the_query->have_posts() ) {
    while ( $the_query->have_posts() ) {
	$the_query->the_post();
	update_field('hide_from_listing', 0);
    }
}

wp_reset_postdata();

Leave a comment

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