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).

So the code above does nothing on it's own, we need to use add_action to hook it up:

Right now, if you Edit a category you'll see something like this:

Edit Category Custom Metadata WordPress

Save the metadata

So basically now we have a form which doesn't get stored in the database. Not very useful, right?

We need to tell WordPress to save this custom metadata on the save action, if he finds it:

With this, the Edit category process works.

What about the Create Category Form?

You can also add the field to the create form. However the HTML wrappers are not the same: for the edit form you wrap the fields on a TR but in the create form you need a DIV.

Because I didn't want to duplicate my HTML field I just didn't add it to the create form. In case you want to do it, here's how:

Custom Columns for the Category list

This final step is optional, perhaps you don't need it. However, I think it's nice to present this new field in the category list (like in the first screenshot at the top).

How do we do that?

First of all add the column header:

This code just ouputs the Home Featured column, with no values in it.

With the following snippet we'll retrieve the metadata from the database:

That's it. I hope I explained myself, make sure to check the WordPress Codex if you have any issue with the functions.

  • 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.

  • 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.

  • 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.

    Waiting for your quick reply! 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?

Leave a Reply

Add <code> Some Code </code> by using this tags.