WPBeginner

Beginner's Guide for WordPress

  • Blog
    • Beginners Guide
    • News
    • Opinion
    • Showcase
    • Themes
    • Tutorials
    • WordPress Plugins
  • Start Here
    • How to Start a Blog
    • Create a Website
    • Start an Online Store
    • Best Website Builder
    • Email Marketing
    • WordPress Hosting
    • Business Name Ideas
  • Deals
    • Bluehost Coupon
    • SiteGround Coupon
    • WP Engine Coupon
    • HostGator Coupon
    • Domain.com Coupon
    • Constant Contact
    • View All Deals »
  • Glossary
  • Videos
  • Products
X
☰
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

WPBeginner» Blog» Tutorials» How to Add Custom Meta Fields to Custom Taxonomies in WordPress

How to Add Custom Meta Fields to Custom Taxonomies in WordPress

Last updated on October 18th, 2012 by Editorial Staff
23 Shares
Share
Tweet
Share
Pin
Free WordPress Video Tutorials on YouTube by WPBeginner
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.

23 Shares
Share
Tweet
Share
Pin
Popular on WPBeginner Right Now!
  • Google Analytics in WordPress

    How to Install Google Analytics in WordPress for Beginners

  • How to Properly Move Your Blog from WordPress.com to WordPress.org

  • Checklist

    Checklist: 15 Things You MUST DO Before Changing WordPress Themes

  • Revealed: Why Building an Email List is so Important Today (6 Reasons)

    Revealed: Why Building an Email List is so Important Today (6 Reasons)

About the Editorial Staff

Editorial Staff at WPBeginner is a team of WordPress experts led by Syed Balkhi. Trusted by over 1.3 million readers worldwide.

The Ultimate WordPress Toolkit

45 Comments

Leave a Reply
  1. Tarek Anandan says:
    Feb 1, 2016 at 8:24 pm

    If you can’t your data to display and you’re using a recent version of WordPress, use get_term_meta() instead of get_tax_meta().

    Reply
  2. Subin says:
    Jan 19, 2016 at 6:09 am

    here’s my code:
    $config = array(
    ‘id’ => ‘tax_meta_box’, // meta box id, unique per meta box
    ‘title’ => ‘Tax Meta Box’, // meta box title
    ‘pages’ => array(‘category’), // taxonomy name, accept categories, post_tag and custom taxonomies
    ‘context’ => ‘normal’, // where the meta box appear: normal (default), advanced, side; optional
    ‘fields’ => array(), // list of meta fields (can be added by field arrays)
    ‘local_images’ => false, // Use local or hosted images (meta box images for add/remove)
    ‘use_with_theme’ => get_template_directory_uri() . ‘/Tax-Meta-Class/Tax-meta-class’
    );
    /*
    * Initiate your meta box
    */
    $my_meta = new Tax_Meta_Class($config);
    //Image field
    $my_meta->addImage(‘image_field’,array(‘name’=> __(‘My Image ‘,’tax-meta’)));

    $my_meta->Finish();
    ———————————————————————————————————————–
    term_id;
    $saved_data = get_tax_meta($term_id,’image_field’,true);
    echo ”;
    echo $term_id; // Echoes ‘1’ which is the category ID
    echo $saved_data; // Doesn’t show anything . Wonder Why!!
    exit;

    ?>

    Reply
  3. James Dower says:
    Nov 23, 2015 at 1:25 pm

    I’m also having issues getting the path for an uploaded image. It just echos:

    Array

    …text fields are fine.

    I’m using:

    $prefix = ‘ttp_’;

    $my_meta->addImage($prefix.’logo_id’,array(‘name’=> __(‘Logo’,’tax-meta’)));

    And on the taxonomy.php page:

    $logo = get_tax_meta($term->term_id, ttp_logo_id);

    Can anyone help please? I’ve been at this for HOURS!

    Reply
    • James Dower says:
      Nov 23, 2015 at 1:33 pm

      …and is anyone please able to explain how to echo repeater fields?!

      Reply
  4. James Dower says:
    Nov 23, 2015 at 12:50 pm

    For all those struggling to get this to read out, I found a stray apostrophe in there, this works for me:

    $data = get_tax_meta($term->term_id, ba_text_field_id);
    echo $data;

    Reply
  5. siva says:
    Oct 24, 2015 at 7:45 am

    Hi Team very very thank you…..

    Reply
  6. Ketan says:
    Sep 1, 2015 at 6:18 am

    Hi,

    I am activate plugin. but i don’t show where i can add custom taxonomy filed in admin side. please replay me.

    Thanks,
    Ketan.

    Reply
  7. Francisco says:
    Jul 17, 2015 at 1:58 am

    Thanks a lot this has been very useful!!!! :D

    I displayer correclty a text field using this code:
    //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,’ba_text_field_id’);
    echo $saved_data;

    BUT I need to display an image too, I’m using this php code in my template:

    //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,’ba_image_field_id’);
    echo $saved_data;

    But it doesn’t display anything! :(

    It just shows a single word “array”

    What could be wrong?

    Reply
  8. David Beastall says:
    Mar 14, 2015 at 7:40 pm

    Thanks for that, the tutorial seems incomplete imo.

    Reply
  9. Hemanta Chakladar says:
    Mar 8, 2015 at 6:58 am

    I’m also searching for that.

    Reply
  10. Hemanta Chakladar says:
    Mar 8, 2015 at 6:56 am

    I’m stuck.

    I can see meta data option in wordpress category and not in my custom taxonomy. Where can I add this option to add custom meta in my custom taxonomy ? Where that is defined ?

    Reply
  11. Marco Panichi says:
    Feb 24, 2015 at 6:09 am

    How to add a multiple checkbox in order to select one or more users?

    Reply
    • Ankur says:
      May 27, 2016 at 10:25 pm

      Hi Marco, Did you get any answer on it? I am also looking for the same functionality.

      Reply
  12. Joe Chow says:
    Feb 10, 2015 at 5:05 am

    thanks a lot.I can see there are bunch of fields showed at the post category. However, I just want to know how to make the code to be used for woocommerce product category only.

    Can anyone give a tip?

    Reply
  13. Joe says:
    Jan 27, 2015 at 12:53 pm

    How would you get the taxonomy information on a custom page template instead of a taxonomy.php page?

    Reply
  14. Jason Gateman says:
    Dec 5, 2014 at 6:14 pm

    Thanks for this tutorial! How do I get the meta to show on a custom post page I have created as opposed to just the taxonomy page?

    Reply
  15. AD says:
    Aug 25, 2014 at 2:53 am

    Hi Guys,

    For those who are having issues with displaying the content that is saved. Use the prefix in front of the key.

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

    That should be ba_text_field_id (Given that the prefix is ba_) .

    Hope this helps cheers.

    Reply
    • Dan says:
      Sep 1, 2015 at 4:34 pm

      I wanted to second this comment, as it can be quite puzzling to figure out why the example doesn’t work. If you look in the “Tax meta class” file you will see at the top that Ohad defines a $prefix variable that is appended to each save function.

      So in order to get the result to echo out, you need to include the prefix. You can set the prefix to anything you want, including “”. I have no idea what “ba_” stands for, but that is what is in there by default.

      The example should read:
      $saved_data = get_tax_meta($term->term_id,’ba_text_field_id’);

      Reply
  16. Chris says:
    Apr 1, 2014 at 5:06 pm

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

    Reply
  17. Ante says:
    Jan 29, 2014 at 6:41 am

    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’ ?????

    Reply
    • Steve says:
      Feb 19, 2014 at 7:56 pm

      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;

      Reply
  18. fp-admin says:
    Jan 23, 2014 at 12:31 pm

    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="”>

    Reply
  19. ELiane says:
    Sep 25, 2013 at 11:34 am

    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

    Reply
    • WPBeginner Support says:
      Sep 25, 2013 at 11:16 pm

      yes if you have followed all instructions as described above, then it should work.

      Reply
  20. Tejas Deshmukh says:
    Sep 23, 2013 at 2:54 am

    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?

    Reply
  21. alexut says:
    Aug 5, 2013 at 3:34 pm

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

    Reply
  22. Kisshomaru says:
    Jun 6, 2013 at 6:23 pm

    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!

    Reply
  23. Mike Jackson says:
    May 30, 2013 at 8:18 am

    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

    Reply
  24. Mike Jackson says:
    May 29, 2013 at 2:28 pm

    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?

    Reply
    • Mike Jackson says:
      May 29, 2013 at 3:00 pm

      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;

      ?>

      Reply
      • Editorial Staff says:
        Jun 16, 2013 at 2:55 pm

        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

        Reply
  25. FJ says:
    May 22, 2013 at 4:33 pm

    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.

    Reply
    • Editorial Staff says:
      May 23, 2013 at 3:23 pm

      In line 24 of example-usage.php, you should be able to specify your custom taxonomy.

      Reply
      • Ben says:
        Oct 20, 2015 at 5:59 am

        To clarify – Line 24 of taxonomy-fields.php

        ‘pages’ => array(‘category’), // taxonomy name, accept categories, post_tag and custom taxonomies

        CHANGE the XXX to whatever your custom taxonomy is called
        ‘pages’ => array(‘xxxxx’), // taxonomy name, accept categories, post_tag and custom taxonomies

        Reply
  26. Kobbe says:
    May 9, 2013 at 5:56 pm

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

    Reply
    • Editorial Staff says:
      May 12, 2013 at 8:13 am

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

      Reply
      • Kobbe says:
        May 12, 2013 at 7:11 pm

        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

        Reply
        • Editorial Staff says:
          May 13, 2013 at 10:40 am

          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.

  27. gaurav kaushik says:
    Jan 19, 2013 at 8:13 am

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

    Reply
    • Editorial Staff says:
      Jan 19, 2013 at 7:54 pm

      It does it in the options table.

      Reply
      • Gaurav Kaushik says:
        Feb 6, 2013 at 8:24 am

        can we change the backend of this plugin..i mean we want to create a termmeta table (meta_id,term_id,meta_key,meta_value) ..and save the taxonomy meta here…or connect it with this plugin? http://wordpress.org/extend/plugins/simple-term-meta/

        Reply
  28. Jim says:
    Oct 25, 2012 at 6:43 pm

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

    Reply
  29. Michelle says:
    Oct 19, 2012 at 1:25 am

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

    Reply
  30. Jaime says:
    Oct 18, 2012 at 11:04 pm

    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!

    Reply
    • Editorial Staff says:
      Oct 18, 2012 at 11:27 pm

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

      Reply

Leave a Reply Cancel reply

Thanks for choosing to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and your email address will NOT be published. Please Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

Over 1,320,000+ Readers

Get fresh content from WPBeginner

Featured WordPress Plugin
SeedProd Logo
SeedProd
Create beautiful custom landing pages - Drag & drop builder. Learn More »
How to Start a Blog How to Start a Blog
I need help with ...
Starting a
Blog
WordPress
Performance
WordPress
Security
WordPress
SEO
WordPress
Errors
Building an
Online Store
Useful WordPress Guides
    • 7 Best WordPress Backup Plugins Compared (Pros and Cons)
    • How to Fix the Error Establishing a Database Connection in WordPress
    • Why You Need a CDN for your WordPress Blog? [Infographic]
    • 30 Legit Ways to Make Money Online Blogging with WordPress
    • Self Hosted WordPress.org vs. Free WordPress.com [Infograph]
    • Free Recording: WordPress Workshop for Beginners
    • 24 Must Have WordPress Plugins for Business Websites
    • How to Properly Move Your Blog from WordPress.com to WordPress.org
    • 5 Best Contact Form Plugins for WordPress Compared
    • Which is the Best WordPress Popup Plugin? (Comparison)
    • Best WooCommerce Hosting in 2020 (Comparison)
    • How to Fix the Internal Server Error in WordPress
    • How to Install WordPress - Complete WordPress Installation Tutorial
    • Why You Should Start Building an Email List Right Away
    • How to Properly Move WordPress to a New Domain Without Losing SEO
    • How to Choose the Best WordPress Hosting for Your Website
    • How to Choose the Best Blogging Platform (Comparison)
    • WordPress Tutorials - 200+ Step by Step WordPress Tutorials
    • 5 Best WordPress Ecommerce Plugins Compared
    • 5 Best WordPress Membership Plugins (Compared)
    • 7 Best Email Marketing Services for Small Business (2020)
    • How to Choose the Best Domain Registrar (Compared)
    • The Truth About Shared WordPress Web Hosting
    • When Do You Really Need Managed WordPress Hosting?
    • 5 Best Drag and Drop WordPress Page Builders Compared
    • How to Switch from Blogger to WordPress without Losing Google Rankings
    • How to Properly Switch From Wix to WordPress (Step by Step)
    • How to Properly Move from Weebly to WordPress (Step by Step)
    • Do You Really Need a VPS? Best WordPress VPS Hosting Compared
    • How to Properly Move from Squarespace to WordPress
    • How to Register a Domain Name (+ tip to get it for FREE)
    • HostGator Review - An Honest Look at Speed & Uptime (2020)
    • SiteGround Reviews from 4196 Users & Our Experts (2020)
    • Bluehost Review from Real Users + Performance Stats (2020)
    • How Much Does It Really Cost to Build a WordPress Website?
    • How to Create an Email Newsletter the RIGHT WAY (Step by Step)
    • Free Business Name Generator (A.I Powered)
    • How to Create a Free Business Email Address in 5 Minutes (Step by Step)
    • How to Install Google Analytics in WordPress for Beginners
    • How to Move WordPress to a New Host or Server With No Downtime
    • Why is WordPress Free? What are the Costs? What is the Catch?
    • How to Make a Website in 2020 – Step by Step Guide
Deals & Coupons (view all)
MonsterInsights Coupon Code
MonsterInsights Coupon
Get 50% off MonsterInsights, the best Google Analytics plugin for WordPress.
Imagely Coupon
Get 20% OFF on Imagely WordPress photography themes and plugins.
Featured In
About WPBeginner®

WPBeginner is a free WordPress resource site for Beginners. WPBeginner was founded in July 2009 by Syed Balkhi. The main goal of this site is to provide quality tips, tricks, hacks, and other WordPress resources that allows WordPress beginners to improve their site(s).
Join our team: We are Hiring!

Site Links
  • About Us
  • Contact Us
  • FTC Disclosure
  • Privacy Policy
  • Terms of Service
  • Free Blog Setup
  • Free Business Tools
Our Sites
  • OptinMonster
  • MonsterInsights
  • WPForms
  • SeedProd
  • Nameboy
  • RafflePress
  • Smash Balloon

Copyright © 2009 - 2021 WPBeginner LLC. All Rights Reserved. WPBeginner® is a registered trademark.

Managed by Awesome Motive | WordPress hosting by SiteGround | WordPress CDN by MaxCDN | WordPress Security by Sucuri.