Add Metadata to Categories – WordPress

If you're reading this chances are you have the same problem I had.

How do I add custom metadata to a category?

I needed to add a simple dropdown to categories in order to set them as featured. Then I wanted to use this featured field to show them in the home page in a specific location.

Well, after a lot of reading I didn't find any blog post or tutorial that was clean. I've found plugins and big chunks of code, unbearable.

I'll do my best to explain you the main functions you need to achieve this:

Custom Metadata Category WordPress

Plugin or functions.php?

First of all you need to decide if you want to create a plugin or add this code to your functions.php file. For my project I decided to use the functions.php since it's a custom code specific for this project, unlikely to be reused in the same way.

How to use the bits of code below?

I'll explain part by part but you can copy them all and paste them in order inside your functions.php file, I'll probably work (at the time of the writing we had WordPress 3.8).

Add your new field to the Edit form

We'll add a new TR with our dropdown. Pay special attention to the select name and id, which is called: term_meta[featured] (our new metadata field is featured).

<?php function xg_edit_featured_category_field( $term ){ $term_id = $term->term_id; $term_meta = get_option( "taxonomy_$term_id" ); ?> <tr class="form-field"> <th scope="row"> <label for="term_meta[featured]"><?php echo _e('Home Featured') ?></label> <td> <select name="term_meta[featured]" id="term_meta[featured]"> <option value="0" <?=($term_meta['featured'] == 0) ? 'selected': ''?>><?php echo _e('No'); ?></option> <option value="1" <?=($term_meta['featured'] == 1) ? 'selected': ''?>><?php echo _e('Yes'); ?></option> </select> </td> </th> </tr> <?php } add_action( 'category_edit_form_fields', 'xg_edit_featured_category_field' ); // Save the field function xg_save_tax_meta( $term_id ){ if ( isset( $_POST['term_meta'] ) ) { $term_meta = array(); // Be careful with the intval here. If it's text you could use sanitize_text_field() $term_meta['featured'] = isset ( $_POST['term_meta']['featured'] ) ? intval( $_POST['term_meta']['featured'] ) : ''; // Save the option array. update_option( "taxonomy_$term_id", $term_meta ); } } // save_tax_meta add_action( 'edited_category', 'xg_save_tax_meta', 10, 2 ); add_action( 'category_add_form_fields', 'xg_edit_featured_category_field' ); // Add column to Category list function xg_featured_category_columns($columns) { return array_merge($columns, array('featured' => __('Home Featured'))); } add_filter('manage_edit-category_columns' , 'xg_featured_category_columns'); // Add the value to the column function xg_featured_category_columns_values( $deprecated, $column_name, $term_id) { if($column_name === 'featured'){ $term_meta = get_option( "taxonomy_$term_id" ); if($term_meta['featured'] === 1){ echo _e('Yes'); }else{ echo _e('No'); } } } add_action( 'manage_category_custom_column' , 'xg_featured_category_columns_values', 10, 3 );
Code language: PHP (php)

About Ricard Torres

Senior Front-end Software Engineer at Netcentric, from Barcelona, Haidong Gumdo Instructor (korean martial art of the sword), street photographer, travel lover, TV addict, Boston Red Sox fan, and privacy advocate.

@ricard_dev @ricard_dev

📝 Blog 🎙 Podcast

  • Awesome post man, your my hero!!!

    I just have one question, how do I retrieve the data on the front-end?

    Thanks again!

  • I have written almost the same code as that of your but the function call in create_category hook is not getting called.The same function is working good for edited_category hook.Can you advice something?

  • I have another question, how you can sae the info of Input text fields instead a Select? Thanks a lot.

  • Hi Rick,

    As you guide, extra field currently saved to wp-options, however, if I want to sort categories taxonomies base on option values , how can I do ?



  • Hi! Thanks for this great article! It really made my day! once again thanks for this free treat!

    So far its working just awesome! But, I have come to a hold and maybe you can help me here.

    I have 10 options in the dropdown field, and the dropdown field is working just fine. but, how do I show the value on the column? currently, its showing “No”, because of the “if” statement I have in the code for “Yes” and “No”. And in my case I have 10 options so, I guess you would help me figure out how do I show the 10 options as saved by each category on the column?

  • Woww!! Excellent and Marvellous!! I just did it!

    All thanks to you and some credit goes to for the answer to my question!

    now I have code that’s working just excellent. I am posting it here so that maybe future users can benefit from it too.

    // Add the value to the column function xg_featured_category_columns_values( $deprecated, $column_name, $term_id) { if($column_name === 'featured'){ $term_meta = get_option( "taxonomy_$term_id" ); switch ($term_meta['featured']){ case "0": echo "0"; break; case "1": echo "1"; break; case "2": echo "2"; break; case "3": echo "3"; break; case "4": echo "4"; break; case "5": echo "5"; break; case "6": echo "6"; break; case "6": echo "6"; break; case "7": echo "7"; break; case "8": echo "8"; break; case "9": echo "9"; break; case "10": echo "10"; break; default: echo "None"; } } } add_action( 'manage_category_custom_column' , 'xg_featured_category_columns_values', 10, 3 );
    Code language: PHP (php)
  • Hey, sorry for posting twice here but I think some codes are being erased while posting!! Thats strange. ok, no problem I will post it here as a simple texts.

    function xg_edit_featured_category_field( $term ){ $term_id = $term->term_id; $term_meta = get_option( “taxonomy_$term_id" ); ?> <option value="0" > <option value="1" > <option value="2" > <option value="3" > <option value="4" > <option value="5" > <option value="6" > <option value="7" > <option value="8" > <option value="9" > <option value="10" > <?php }
    Code language: HTML, XML (xml)
  • OMG!! sorry! Again it erased my codes while posting comments here. please delete my half posted comments, as it will be of no use to anyone.

    I hope atleast I made you understand how I did it!!

    Again, lots of thanks for your grand code and this awesome article!

  • Okay, I have one more question, which I am trying to solve from more than 10 hours but with no success!! I am sure you will help me out here.

    1. How do I display the field on the frontend?

    2. Say, I want to pass an argument to the page, for example, I have this code in my hand which is a normal custom field of a single post in working condition, and I want to achieve a similar solution through your code.

    if ( get_field( 'my_key' ) == sunny ) { get_template_part( 'single/single', 'modern' ); }
    Code language: JavaScript (javascript)

    Waiting for your quick reply! Thanks!!

    • And ofcourse, I am using your code in my archive.php page to redirect users to some other pages based on what the admin’s choice is on the category page!

      And one more thing, how do I output the field classes of your code in my body tag?

      For example, I have this code and I want to achieve similar solution.

      // My Key as class name if ( is_single() ) { $post_name = $post->my_key; $classes[] = 'post-'.$post_name; }
      Code language: PHP (php)

      Any idea would be very much appreciated. Thanks!

  • Great tutorial – i do have a problem while saving.

    “Warning: Cannot modify header information – headers already sent by” after updating the form. You have an idea?

  • I was looking for this solution, Though this get’s the work done. The solution is very inefficient. It will query to the MySQL server every time you need to read from the database. So, If you have 100 category , even if you want to display this to the same page, You will need to query 100 times extra to get your data.

Leave a Reply

Add <code> Some Code </code> if you need to.