How to Bulk Update WordPress Posts and ACF Fields Using functions.php

In this comprehensive tutorial, you will learn how to efficiently update multiple WordPress posts and their corresponding Advanced Custom Fields (ACF) using a custom function in your theme’s functions.php file. Whether you’re managing default posts or custom post types with ACF, this guide will walk you through the process step by step. By the end, you’ll have a clear understanding of how to programmatically update posts and ACF fields in bulk without relying on plugins. Let’s get started!

Before proceeding with any customizations in WordPress, it’s essential to set up a child theme. A child theme acts as a safe and efficient way to make modifications without affecting the parent theme. If you haven’t set up a child theme yet, follow this tutorial on How to Create a Child Theme for Customization. It will guide you through the process and ensure that your customizations remain intact even after theme updates.

Edit functions.php File and Add Custom Bulk Update Function

Begin by accessing your WordPress theme’s functions.php file. This file acts as the nerve center for customizing your theme’s functionality, safeguarding your modifications from being overwritten during updates. You can edit this file using a code editor or directly from your WordPress dashboard using the Theme Editor.

After gaining access to the functions.php file, add the following code snippet at the end. This code will enable you to perform bulk updates on your WordPress posts efficiently:

function custom_bulk_update_posts() {
	$args      = array(
		'post_type'   => 'post', // Specify the post type you want to update, e.g., 'post', 'events', 'books'
		'numberposts' => -1,
	);
	$all_posts = get_posts( $args );

	foreach ( $all_posts as $single_post ) {
		$updated_post = array(
			'ID'         => $single_post->ID,
			'post_title' => $single_post->post_title . '', // Append an empty string to the post title
		);
		wp_update_post( $updated_post );
	}
}

add_action( 'wp_loaded', 'custom_bulk_update_posts' );
Code language: PHP (php)

Explanation:

  • function custom_bulk_update_posts() { ... }: This defines a function named custom_bulk_update_posts.
  • $args: An array specifying the parameters for fetching posts. 'post_type' => 'post' indicates that it will fetch posts of the type ‘post’. 'numberposts' => -1 means it will fetch all posts.
  • $all_posts = get_posts($args);: This retrieves all posts based on the provided arguments and stores them in the $all_posts variable.
  • foreach ($all_posts as $single_post) { ... }: This loop iterates through each post fetched.
  • $updated_post: An array containing the updated post’s ID and modified title.
  • 'ID' => $single_post->ID: Specifies the ID of the current post being processed.
  • 'post_title' => $single_post->post_title . '': Modifies the post title by appending an empty string, effectively leaving the title unchanged.
  • wp_update_post($updated_post);: This function updates the post with the modified title.

In essence, this code fetches all posts of the type ‘post‘, modifies their titles by adding an empty string, and updates each post with the modified title. When you visit your website, the wp_loaded event triggers this function, initiating the bulk update process for the specified post type.

Save and Visit Your Website

After adding the code, save the functions.php file. Now, visit your website. This action triggers the wp_loaded event and initiates the bulk update process for the specified post type.

Check and Disable the Function

After visiting your website and confirming that the posts are updated, you can disable the function. Immediate removal can be done by commenting out the add_action line in functions.php:

// add_action('wp_loaded', 'custom_bulk_update_posts');
Code language: PHP (php)

Alternatively, for a long-term solution, you can remove the custom_bulk_update_posts function permanently.

Updating ACF Fields (Optional)

Now, let’s extend our bulk update functionality to include Advanced Custom Fields (ACF). If you have custom fields created using ACF, you can update them alongside your posts. Here’s how:

Modify the Custom Bulk Update Function

Inside the foreach loop of the custom_bulk_update_posts function,you can add ACF fields. Here is the full code again, replace 'your_acf_field_name' with the name of your ACF field:

function custom_bulk_update_posts() {
	$args      = array(
		'post_type'   => 'post', // Specify the post type you want to update, e.g., 'post', 'events', 'books'
		'numberposts' => -1,
	);
	$all_posts = get_posts( $args );

	foreach ( $all_posts as $single_post ) {
		$updated_post = array(
			'ID'         => $single_post->ID,
			'post_title' => $single_post->post_title . '', // Append an empty string to the post title
		);

		// Get and update ACF field values
		$acf_field_value = get_field( 'your_acf_field_name', $single_post->ID );
		// Modify the ACF field value if necessary
		// For example, add ' Updated' to the ACF field
		$updated_acf_value = $acf_field_value . ' Updated';
		update_field( 'your_acf_field_name', $updated_acf_value, $single_post->ID );

		wp_update_post( $updated_post );
	}
}

add_action( 'wp_loaded', 'custom_bulk_update_posts' );
Code language: PHP (php)

Code Explanation:

  • function custom_bulk_update_posts() { ... }: Defines a custom function named custom_bulk_update_posts. Functions in PHP allow you to encapsulate a set of instructions for later use.
  • $args = array( 'post_type' => 'post', 'numberposts' => -1, );: Creates an array named $args, specifying parameters for fetching posts.
  • 'post_type' => 'post' fetches posts of the type ‘post’.
  • 'numberposts' => -1 means it will fetch all posts regardless of the number.
  • $all_posts = get_posts($args);: Retrieves all posts based on the provided arguments and stores them in the variable $all_posts.
  • foreach ($all_posts as $single_post) { ... }: Iterates through each post fetched in the $all_posts array, allowing you to process each post individually.
  • $updated_post = array( 'ID' => $single_post->ID, 'post_title' => $single_post->post_title . '', );: Creates an array named $updated_post containing the post’s ID and the modified post title.
  • 'ID' => $single_post->ID specifies the ID of the current post being processed.
  • 'post_title' => $single_post->post_title . '' appends an empty string to the original post title, leaving it unchanged.
  • $acf_field_value = get_field('your_acf_field_name', $single_post->ID);: Retrieves the value of the specified ACF field for the current post.
  • $updated_acf_value = $acf_field_value . ' Updated';: Modifies the retrieved ACF field value by appending ‘ Updated’ to it.
  • update_field('your_acf_field_name', $updated_acf_value, $single_post->ID);: Updates the specified ACF field with the modified value for the current post.
  • wp_update_post($updated_post);: Updates the post with the modified title.
  • add_action('wp_loaded', 'custom_bulk_update_posts');: Hooks the custom_bulk_update_posts function to the wp_loaded action. This means the function will be executed when WordPress has finished loading, allowing for bulk updates when you visit your website.

Updating Multiple Post Types (Optional)

If you want to update multiple post types simultaneously, modify the code snippet as follows:

function custom_bulk_update_posts() {
	$args      = array(
		'post_type'   => array( 'post', 'events', 'books' ), // Specify multiple post types
		'numberposts' => -1,
	);
	$all_posts = get_posts( $args );

	foreach ( $all_posts as $single_post ) {
		$updated_post = array(
			'ID'         => $single_post->ID,
			'post_title' => $single_post->post_title . '', // Append an empty string to the post title
		);
		wp_update_post( $updated_post );
	}
}

add_action( 'wp_loaded', 'custom_bulk_update_posts' );
Code language: PHP (php)

Explanation:

  • 'post_type' => array('post', 'events', 'books'): Specifies an array of post types to update.
  • The rest of the code functions similarly to the single post type example.

That’s it! You’ve successfully enhanced your bulk update functionality to include Advanced Custom Fields (ACF). Now, your WordPress posts and their associated ACF fields can be efficiently updated in bulk. This method provides you with a powerful tool to manage your content effectively without relying on plugins. Always customize the code according to your specific post types and ACF field names, and remember to test in a staging environment before implementing changes on your live website.

Leave your feedback and help us improve 🐶

We hope you found this article helpful! If you have any questions, feedback, or spot any errors, please let us know in the comments. Your input is valuable and helps us improve. If you liked this article, please consider sharing it with others. And if you really enjoyed it, you can show your support by buying us a cup of coffee ☕️ or donating via PayPal 💰.

More free knowledge, because why not?

Your thoughts matter, leave a reply 💬

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