WordPress function to delete empty custom fields from posts

The following WordPress PHP function allows you to delete custom fields from your posts that are empty across all posts.

Custom fields are a powerful feature in WordPress, allowing you to store additional metadata with your posts. However, over time, you may end up with a large number of empty or unused custom fields that clutter your database. This function will help you clean up your custom fields and keep your WordPress installation running smoothly.

WordPress PHP function

add_action( 'admin_init', 'delete_custom_fields_on_admin_init' );
function delete_custom_fields_on_admin_init() {
	
	// Set log file path
	$log_file = '../delete_custom_fields_on_admin_init.txt';
	
	// Get current date and time
	$datetime = date( 'Y-m-d H:i:s' );
	
	$log_data = "\n\n". $datetime;
	
	// Custom fields to be deleted
	$custom_fields = array(
		'dsq_thread_id',
		'_yoast_wpseo_content_score',
		'ampforwp-amp-on-off'
	);
	
	$post_types = array( 'post' );
	
	foreach ( $post_types as $post_type ) {
		// Function to delete custom fields for post type
		$args = array(
			'post_type' => $post_type,
			'post_status' => 'any',
			'fields' => 'ids',
			'posts_per_page' => -1
		);

		$query = new WP_Query($args);

		if ($query->have_posts()) {
			while ($query->have_posts()) {
				$query->the_post();
				foreach ($custom_fields as $field) {
					if (delete_post_meta(get_the_ID(), $field)) {
						// Log a message indicating the custom field was deleted
						$log_data .= '\nDeleted custom field ' . $field . ' for post ' . get_the_ID() . '\n';
					}
				}
			}
			wp_reset_postdata();
		}
	}
	
	// Write to log file
	if ( ! empty( $log_data ) ) {
		file_put_contents( $log_file, $log_data, FILE_APPEND );
	}
}

How does the function work?

The function is triggered during the ‘admin_init’ action hook, which is called when an administrator opens into the WordPress admin panel.

The function:

  1. Loops through specified post types (in this example, ‘post’) and fetches all posts.
  2. Retrieves custom field data from each post.
  3. Counts the number of non-empty occurrences of each custom field.
  4. Logs the count of non-empty occurrences for each custom field.
  5. Deletes empty custom fields from the posts and logs the deleted fields with corresponding post IDs.
  6. Appends the log data to a specified text file (custom-field-deletion-log.txt).

How to use the function?

Important: remember to remove this function after you have used it.

To use the function:

  1. Copy the provided code to your theme’s functions.php file or a custom plugin file.
  2. If you want to target other post types, update the $post_types array accordingly. For example:
$post_types = array( 'post', 'page', 'custom_post_type' );
  1. Save your changes and log in to your WordPress admin panel. The function will run automatically and delete the specified custom fields from the targeted post types.
  2. To check the deletion process, open the specified log file (in this case, ‘../custom-field-deletion-log.txt’) and review the log data. It should contain information about the deleted custom fields and their associated post IDs.