Add a meta field to all posts in a category

Add a meta field to all posts in a category

While adding a meta field and a meta value to a post it’s fairly easy and can even be done from the usual Add post screen, what if you posted few hundred posts and you realize you would need a meta field to be added to all these posts?

The good news is that it can be done automatically with minimal code added to functions.php.

First of all, the WordPress function that adds meta fields and values to a post is add_post_meta. To add a meta field and value to a post from code, you should use the following parameters:

<?php add_post_meta($post_id, $meta_key, $meta_value, $unique); ?>

For example, to add the meta_key “visible” and the value “true” to post 396 you should do:

<?php add_post_meta(396, 'visible', 'true'); ?>

Now, if we use the above way to add a meta_key, then, each time the code is executed (each page load) it will add a meta_key and value to the post 396, so we will end up with a lot of duplicates. To have the meta_key added only one time, we should use the $unique parameter. The code becomes:

<?php add_post_meta(396, 'visible', 'true', true); ?>

Adding the meta_key to all posts in a category

Guess what. To add the same meta_key and value to all posts in a category, you need to make a WordPress loop that goes through all the posts in that category. Let’s say you want the meta_key to be added to all posts in category “example”. Let’s make a loop and replace the hard-coded 396 post id with the id’s from the loop:


global $post;
$example = new WP_Query(array('nopaging' => true,'category_name' => 'example'));

  while ( $example->have_posts() ) : $example->the_post();

    add_post_meta($post->ID, 'visible', 'true', true);

  endwhile;

A few things to note: ‘nopaging’ => true tells the Loop to go through all posts in the category (otherwise the while will be executed for the first 10 posts (or how many posts you usually display in a posts list). category_name is actually the category slug. Also, a good idea is to restrict the execution of the script to admins only, so you could add if(is_admin()) { –code– }.

Executing the script when the Admin bar menu is displayed

Now that we have a running script that will add the meta_key and meta_value to all posts in a category, we should execute the script using add_action and putting the code to be executed in a function.


function add_meta_to_posts(){

 if(is_admin()) {

  global $post;


  $example = new WP_Query(array('nopaging' => true,'category_name' => 'example'));


   while ( $example->have_posts() ) : $example->the_post();

    add_post_meta($post->ID, 'visible', 'true', true);

   endwhile;

 }

}

add_action( 'admin_init', 'add_meta_to_posts', 1 );

 

Stopping the function

After you check that the meta_key and the meta_value are added to the posts in the category, it’s very important to stop the function. Otherwise, each time the admin is loaded a loop that goes through all the posts in the categories is executed. To do this you should simply comment the add_action line.

//add_action( 'admin_init', 'add_meta_to_posts', 1 );