Beginner's Guide for WordPress - Start your WordPress Blog in minutes.
Choosing the Best
WordPress Hosting
How to Easily
Install WordPress
Recommended
WordPress Plugins
View all Guides

How to Add Custom Meta Fields to Custom Taxonomies in WordPress

Last updated on by
Elegant Themes
How to Add Custom Meta Fields to Custom Taxonomies in WordPress

By default WordPress taxonomies (categories, tags, etc) have the fields name, slug, parent, and description. Recently while working on a client’s project, we found a need to add custom meta fields to custom taxonomies. We needed a way to add custom text on each taxonomy archive page. One way would be to hard code the text using the conditional statements in our taxonomy-{name}.php file. That would be a very inefficient way of doing so, and it would give our client no way to modify the text in the future. So we decided to future proof the situation by adding custom meta fields to custom taxonomies. In this article, we will show you how to add additional custom meta fields to custom taxonomies.

Note: This tutorial is for designers and developers.

While searching for an efficient method, we came across Pippin’s tutorial that shows you how to do this. While his tutorial was great, it required us to write a lot of code. We decided to search a little bit further to see if someone has created an easier way to do this. Perhaps a plugin or a class. Thankfully, we found a solution by Ohad Raz on Github. After going through the same issue, he decided to write a class to make it easy for everyone else (got to love the WordPress community). Thanks Ohad.

In our case, we decided to add this functionality as a plugin rather than in a theme. You can choose the method you like. For the sake of this tutorial, we will go the plugin route.

First thing you need to do is download the Tax-Meta-Class from Github. Create a new folder and call it “taxonomy-fields”. Save the “Tax-meta-class” folder inside that folder.

The zip comes with a file called class-usage-demo.php. Just rename that file, and call it taxonomy-fields.php

Ohad did a great job in documenting the file, so it is pretty self-explanatory. He has examples of all type of fields you can add (text field, textarea, checkbox, select, radio, date, time, color picker, file upload, etc). You don’t have to use all the fields. Simply get rid of the ones you don’t want.

Once you are done adding the fields, upload the taxonomy-fields folder in your plugins folder. Activate the plugin, and add data in your fields.

Now, you are ready to display these additional fields in your taxonomy template. Open your taxonomy template. This would be something like taxonomy-{taxonomy-name}.php file. In there, you can simply add the following:

<?php 

//Get the correct taxonomy ID by slug
$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );

//Get Taxonomy Meta
$saved_data = get_tax_meta($term->term_id,'text_field_id');
echo $saved_data; 

?>

That’s it. These classes make it really easy and improve your workflow. We hope that this tutorial has helped you in adding custom meta fields to custom taxonomies.


Editorial Staff at WPBeginner is a team of WordPress lovers led by Syed Balkhi. Page maintained by Syed Balkhi.

WPBeginner's Video Icon
Our HD-Quality tutorial videos for WordPress Beginners will teach you how to use WordPress to create and manage your own website in about an hour. Get started now »

Comments

  1. Chris says:

    Thanks a lot! Works great. You saved me a lot of time coding.

  2. Ante says:

    Ok, I’m stuck.

    //Get the correct taxonomy ID by slug
    $term = get_term_by( ‘slug’, get_query_var( ‘term’ ), get_query_var( ‘taxonomy’ ) );

    //Get Taxonomy Meta
    $saved_data = get_tax_meta($term->term_id,’text_field_id’);
    echo $saved_data;

    So ‘taxonomy’ is my custom taxonomy, let’s say ‘project_clients’
    ‘text_field_id’ is the id of input field, it’s $prefix.’text_area’…

    What’s ‘term’ ?????

    • Steve says:

      Ante,
      Have you had any progress with this??

      I’m wondering the same question – poured over so many comments and pages talking about this, but cannot find a resolution. Any help would be appreciated if you have found the answer.

      Simply adding the following to my taxonomy.php file is not working:

      //Get the correct taxonomy ID by slug
      $term = get_term_by( ‘slug’, get_query_var( ‘term’ ), get_query_var( ‘taxonomy’ ) );

      //Get Taxonomy Meta
      $saved_data = get_tax_meta($term->term_id,’text_field_id’);
      echo $saved_data;

  3. fp-admin says:

    I am trying to add whatever I put in the WYSIWYG to the front end of the website. I honestly have no clue what I’m doing but this is my code so far:

    term_id,’wysiwyg_field_id’);

    ?>

    <a href="”>

  4. ELiane says:

    Hi,

    I used the plugin but I can get the code on my Taxonomy.php.

    I Use this code to display the field

    term_id,’text_field_id’);
    echo $saved_data;

    ?>
    Do I have to do something else?

    Thanks

  5. Tejas Deshmukh says:

    Hello, Specially Thanks for this Idea. This plugin is working superb. I have added three extra fields in Perticular Term add, edit option. but when i create category in it at that term at that time I can’t store value for that extra three fields. So any suggestion for that?

  6. alexut says:

    But how do I get the data out of the archive page? you should add this also as this is a common request.

  7. Kisshomaru says:

    Brilliant! You have no ideea how much time I spent looking for a way to add “quantities” to “products”. This not being a commerce site, it was cumbersome to have a food post tied to an Ingredients taxonomy and not to be able to modify the number of cucumbers for each post.
    Thank You!

  8. Mike Jackson says:

    Hi – I’m having trouble getting this to work. I have the data fields visible in my custom taxonomy but I cannot call the data in my brand-description.php using the php code you pasted at the bottom of your article. It doesn’t show an error, it just doesn’t work.

    I’d really appreciate some further explanation with this if you have the time.

    Thanks

  9. Mike Jackson says:

    This is quite a find! Been trying to solve this for a few weeks. Any idea how to add the new fields to the columns?

    • Mike Jackson says:

      I’m also having trouble outputting anything on the page. I have this code applied and data in the fields but nothing showing on the site

      term_id,’textarea_field_id’);
      echo $saved_data;

      ?>

      • Editorial Staff says:

        This code works on taxonomy archive page. If it is not displaying then chances are that the code is not able to get the taxonomy ID. At least that would be my guess.

        -Syed

  10. FJ says:

    Well, this sounds nice and did the trick on the categories but for any reason, it didn’t on a custom taxonomy of mine. Did I miss something ? Is there a hook somewhere I didn’t look at ?
    Anyway thanks a lot for those enlightening on Ohad’s work. I hanged around it wondering how on earth I could use it properly. I’m just the do-it-yourself type, not a real developper, I’m sure you’ll have understood already.
    Thanks again.

  11. Kobbe says:

    @ Editor please forgive me, How do i output a text field

    • Editorial Staff says:

      You have to use the PHP code that we shared. Make sure to change the field name to the one that you have.

      • Kobbe says:

        Thanks editor i really appreciate for the reply, This is how class-usage-demo.php – http://pastebin.com/ctZPeS0w

        This is what i’m doing but not displaying/outputting anything what am i doing wrong..? and is it possible to display the code (if possible) anywhere in my theme…Thanks

        • Editorial Staff says:

          The code you have is simply setting up the admin side of things. To display the code, look at the PHP code that we have shared in the article. You have to use that in your taxonomy archive file.

  12. gaurav kaushik says:

    its cool..
    but where it save the data…? in postmeta table or it create a new table?

  13. Jim says:

    Cool stuff. I will implement this on our blog in the future.

  14. Michelle says:

    Oh thanks you so much, I’ve been wondering how to identify tags as being English or Spanish for my bilingual site – this helps enormously :-)

  15. Jaime says:

    I actually just finished coding up custom fields for a custom taxonomy for a project I’m working on. I love the flexibility – will certainly check out the plugin for future projects!

    • Editorial Staff says:

      Yes that is the best part about it. We love stumbling onto libraries like this one which makes the life of developers much easier.

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.