WordPress: Update ACF fields programmatically for all posts

When it comes to managing content on a WordPress site, Advanced Custom Fields (ACF) is a popular tool that allows you to customise and add custom fields to posts and pages. One of the challenges you may encounter is having to update an ACF field across all posts on your website. Fortunately, this can be done easily and efficiently by programming a solution. In this blog post, we will explore how to programmatically update an ACF field for all posts on your WordPress site.

Step 1: Identify the ACF field to be updated:
The first step is to identify the specific ACF field you want to update. This can be a text field, a checkbox, an image field or any other type of ACF field. You must have the name or key for this field as it will be needed in the programmatic update process.

Step 2: Create a custom WordPress plugin or add code to functions.php:
You can create a customised WordPress plugin for this task or add the code directly to your themes functions.php file. If you create a plugin, you can easily activate, deactivate and manage the code separately from your theme.

Step 3: Write code to update the ACF field programmatically:
Now it's time to write the actual code to update the ACF field. You can use WordPress' built-in functions and ACF's to achieve this. Here's an example of what the code might look like:


function update_acf_field_programmatic() {
		$postType = 'test';
		$field = 'activate_test_winner';
		$newValue = 1;
		$args = [ 'post_type' => $postType, 'posts_per_page' => -1, 'fields' => 'ids'];
		$posts = get_posts($args);
		foreach ( $posts as $post ) {
			update_field($field, $newValue, $post);
		}
}

if (isset($_GET['update_acf']) && $_GET['update_acf'] === 'yes') {
	update_acf_field_programmatic();
	echo ('The ACF field has been updated for all posts.');
    wp_die();
}



Be sure to replace "$field" with the actual name of the ACF field you want to update, and "$newValue" with the desired value you want to assign to the field. In addition, remember to select the record type you want to update under "$postType". In the example shown, we are updating the post type "test".

If you don't think the function updates the field correctly or it reports errors, try using "update_post_meta" instead of using "update_field", example: update_post_meta($post, field, newValue);

Step 4: Run the code and the ACF field will be updated.
Once you have implemented the code to update the ACF field programmatically for all posts, you can activate the update using a GET URL parameter. This allows you to easily trigger the update by adding a simple parameter to your URL.

When you visit the specified URL, the code will first check if the GET parameter update_acf is present and has the value yes. If the condition is met, the update process will begin and the ACF field will be updated for all posts on your website.

The url should look like this to update (remember to change it to your website): https://dinhjemmeside.dk/?update_acf=ja

Once the update is complete, you will receive a confirmation message on the screen telling you that the ACF field has been updated for all posts. If the GET parameter is not present or has a value other than yes, another message will appear informing you that no update was performed.

By using this approach, you can easily control when and how the ACF field is updated for all posts on your website. This gives you flexibility and control over the update process and ensures that you only perform the update when necessary.

Remember to remove or disable the update code after performing the update to avoid accidentally updating the ACF field in the future.

Now you are ready to enable the update of the ACF field using a GET URL parameter.

Good luck with your ACF field update! 🙌