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.

7 Responses to “Add Metadata to Categories – WordPress”

  1. Jared July 24, 2014 at 9:35 am #

    Awesome post man, your my hero!!!

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

    Thanks again!

    • Rick July 24, 2014 at 9:45 am #

      Hi Jared,

      Use the get_option function. Here’s a snippet following my post example:

      Useful links:



      • Jared July 24, 2014 at 9:49 am #

        Thanks again Rick, I just stumbled across “get_option”, when looking in the database for the new meta :)

        Your a legend – appreciate the help!

      • Rick July 24, 2014 at 9:50 am #

        You’re welcome Jared!

  2. Alex September 19, 2015 at 5:27 pm #

    Thank you, man

  3. Anjuman Chawla September 22, 2015 at 9:13 am #

    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?

    • Rick September 22, 2015 at 9:35 pm #

      I’m afraid I wouldn’t know.

Leave a Reply

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