Part 3: Custom Fields vs Custom Post Types vs Custom Taxonomies


Part 3 of a Series

This post is the third and final part in a series.  Part 1 is here: Custom Fields. Part 2 is here: Custom Post Types.

Today we’ll go over “custom taxonomies.”

What Exactly does Taxonomy Mean?

Taxonomy is defined as “the branch of science concerned with classification.” So another way to put it is taxonomy means grouping things together – i.e. classifying them.

You’re no doubt already familiar with taxonomies in WordPress:

  • Categories: Your categories are a type of taxonomy. You are grouping different posts together into categories.
  • Tags: Tags are another type of taxonomy. When you create tags for your posts, you are creating groups of posts that are tied together with the tag you gave them.

How Categories and Tags Work on Your Site

I’m sure you know this information, but we’ll go over it here so you’ll have a point of reference fresh in mind for when we start defining WordPress custom taxonomies.

With both categories and tags, you can click on a link and see all the posts in that group. If you have a category called Pizza Restaurants, then you can access all those posts by navigating to the Pizza Restaurants category.

If you have a tag called “budget” (as in budget restaurants), then you can navigate to the tag page for “budget” and see all the posts that you tagged with that term.

Featured Plugin - WordPress Infinite SEO Plugin

Fully integrated with the SEOMoz API, complete with automatic links, sitemaps and SEO optimization of your WordPress setup - this is the only plugin you need to help you rank your site number 1 on Google - nothing else compares.
Find out more

And So What are Custom Taxonomies?

And so “custom taxonomies” work exactly the same way as categories and tags, except they’re “custom,” of course. You get to make them up from scratch. And like categories and tags, you can set them up to appear on your editing screen in either way: in a checkbox fashion with pre-determined terms like categories, or with an empty text box like tags that you can write whatever you like in.

Custom Taxonomies vs. Custom Fields

You may remember in Part 1 of this series, we talked about custom fields and how they are like extra boxes you put on your editing screen. And as you can see above, custom taxonomies also appear on your editing screen as extra boxes. And so because of that, it’s easy to confuse the two.

The simple difference between a custom taxonomy and a custom field is that with a taxonomy you are putting a post into a group that can be accessed as a group. In other words, it’s like putting a post into a special type of category. All the posts in that “category” (with that taxonomy) are linked together just as regular categories and tags link posts together.

The content published from a custom field is not linked together with other posts. It is simply displayed on the page.

So, again, in essence, a custom taxonomy that you make up yourself is really just a special type of category or a special type of tag. And, of course, you can have as many as you like.

Featured Plugin - WordPress Pop-Up Chat Plugin

No javascript required, no third part chat engine, just fully featured chat right in your own database on your own WP sites - couldn't be easier.
Find out more

Why Custom Taxonomies?

So if custom taxonomies are really just like categories and tags, why would you need them when you have categories and tags?

Well, some people don’t. For some, they can make do with categories and tags. But let’s take our hypothetical food-themed site from the previous two posts as an example again.

Even though I only have three different types of posts (restaurant reviews, recipes, nutrition posts), there are A LOT of different pieces of information in each.

If I tried to put everything that I wanted to group together in categories, I would have a large number of categories checked. For example, on one Pizza Restaurant post I might have Pizza, Downtown, Budget, and Walking Distance to Subway all checked in my categories box.

That’s not a problem, of course. I can make as many categories as I like; however, when I print the categories out on my post, they all get printed out together in one line because the code just says, “Get the categories.” And so that’s what it does, it gets every category checked and displays them in a line.

With custom taxonomies, however, I can print out each individual taxonomy wherever I like.

Featured Plugin - WordPress Newsletter Plugin

Now there's no need to pay for a third party service to sign up, manage and send beautiful email newsletters to your subscriber base - this plugin has got the lot.
Find out more


Decision Time: Custom Taxonomy or Custom Field

Another way to think of the difference between a custom taxonomy and a custom field is that a custom taxonomy usually gets printed on the page as a link (grouping together all the other posts with that taxonomy) while a custom field just gets printed on the page plain, without a link.

And so with this in mind, you will need to decide which types of information you want to be linked together in a group, and which types of information you want to just have printed out plain on the page.

In general, you might want to consider linking common terms that you repeat again and again together, and then leave specific things to be printed out without a link.

Choosing Taxonomy or Field: Some Examples

Going back to our restaurant review example, let’s take a look at a few of the different pieces of information we’re going to want to include, and then we’ll decide if we should use a custom taxonomy, a custom field, or both.

  • Address & phone number – (custom field) This is obviously going to be unique to each restaurant, and so we don’t want to link this information together with other posts/restaurants. No other restaurant will have the exact same phone number and address. However, see the next section.
  • Location – (custom taxonomy) Here we could use some general location indicators, such as Downtown, Northside, Southside, Eastside, Westside, etc. For this, it would be a good idea to use a custom taxonomy because then someone could click on the link for Downtown, for example, and see all the restaurants reviewed in the downtown area.
  • Type of cuisine – (custom taxonomy) This might be a good one to use a custom taxonomy. You could have taxonomies such as Pizza, Seafood, Vegetarian, Chinese, Indian, Sandwiches, etc. Again, someone could click on Pizza and be taken to all the other pizza restaurant reviews.
  • Price range – (maybe both) If you are giving exact prices, such as $7.95 – $24.95, then you will want to use custom fields. If you are giving general prices, such as Budget, Mid-range, Expensive, Very Expensive, then you will probably want to use custom taxonomies. That way, someone could click on Budget, for example, and see all the budget restaurant reviews.

Featured Plugin - WordPress Facebook Plugin

Would you like to add Facebook comments, registration, 'Like' buttons and autoposting to your WP site? Well, The Ultimate Facebook plugin has got that all covered!
Find out more

Important Technical Notes

When you set up a taxonomy, you will need to decide if it’s going to be hierarchical (meaning it can have sub groups) or if it will be non-hierarchical.

An easy way to think about this, again, is to think about categories and tags.

The category box is hierarchical. It can have sub-categories (children). But that also means that the choices in the box are set up beforehand. If you are only going to a few choices that you uses again and again, then you will probably want to go with hierarchical.

The tag box is non-hierarchical. With tags, of course, you typically have many more than you do categories.

And so when thinking about whether you want a certain taxonomy to be hierarchical or not, just ask yourself, “Would I make this a category or a tag?”

How to Create a Custom Taxonomy

Plugins

Unless you’re a coder, as in the past two posts, I would suggest going with a plugin that will help you create your taxonomies. There are a number of them out there. Here are two.

More Taxonomies – This plugin (still in beta) is part of a suite of three plugins to deal with custom fields, custom post types, and custom taxonomies. The other two are the More Fields plugin and the More Types plugin.

Magic Fields 2 – I checked this plugin out recently, and it did the job nicely for me without any problems.

The settings on a custom taxonomy plugin will look something like the following. Notice how it lets me choose where which “post types” I want to place the custom taxonomy box in. In this case, I just chose my “review” type posts. So that taxonomy box shows up on my “Review Posts” screen but not on my regular post or page screens.

Manually

If you’d like to do it manually, you can see the WordPress Codex section for it.


Featured Plugin - WordPress Appointments Plugin

Take, set and manage appointments and client bookings without having to leave WordPress. Appointments+ makes it easy.
Find out more

How to Display a Custom Taxonomy

As with  custom fields, you need to insert code in your theme’s files where you want your custom taxonomy content to appear.

Here’s the way I did for the example I used in this post. Here it is again.

This piece of code is for the first line, the “location” line.

<?php echo get_the_term_list( $post->ID, 'location', 'Location: ', ', ', '' ); ?>

You’ll notice that I included some text before the content – I included Location: and then printed the content (in this case it was “Downtown”). If you wanted something else for your text, you would just change my wording (the Location with a capital “L” and the colon.). If you just wanted to print the content without any text before it, then you would just delete that word.

So that’s some basic code you can use. But in my example, you’ll notice that my text before the content is in bold. So actually my real piece of code looks like this (with <strong> around my text).

<?php echo get_the_term_list( $post->ID, 'location', '<strong>Location:</strong> ', ', ', '' ); ?>

And my entire section of code looks like this …

<?php echo get_the_term_list( $post->ID, 'location', '<strong>Location:</strong> ', ', ', '' ); ?>
<br />
<?php echo get_the_term_list( $post->ID, 'price', '<strong>Price:</strong> ', ', ', '' ); ?>
<br />
<?php echo get_the_term_list( $post->ID, 'cuisine', '<strong>Cuisine:</strong> ', ', ', '' ); ?>
<br />
<?php echo get_the_term_list( $post->ID, 'subway', '<strong>Subway:</strong> ', ', ', '' ); ?>
<br />
<br />

Featured Plugin - WordPress Google Maps Plugin

Simply insert google maps into posts, sidebars and pages - show directions, streetview, provide image overlays and do it all from a simple button and comprehensive widget.
Find out more

Using Other Plugins

Once you get your taxonomies set up, there are other types of plugins you can get to display them. Here’s an example of a custom taxonomy widget plugin I tried.

In this case, I decided to show the different locations of the restaurants. This was pulled from my newly created “location” taxonomy.

End of the Series

If you’ve gone through all three parts of this series, hopefully you have a better understanding of how these three confusing terms – custom fields, custom post types, and custom taxonomies – are different, and yet how they can work hand in hand.

And even more importantly, hopefully these posts have given you some ideas for you own site.

If you haven’t yet, be sure to check out Part 1 of the series (Custom Fields) and Part 2 (Custom Post Types).

Featured Plugin - WordPress Q&A Site Plugin

It's now incredibly easy to start your own Q&A site using nothing more than WordPress - The Q&A plugin simply and brilliantly transforms any site, or page, into a perfect support or Q&A environment.
Find out more

Photo: Arrows on grow from BigStock

Comments (44)

  1. In keeping with your food based site, I have a hypothetical.

    I am thinking about a site that will let subscribers select items from a list, say ALL restaurants in town, maybe divided by cuisine. Then if a post (a review or coupon) is later added that is related to that restaurant, the user would be updated.

    My thoughts are to use specific taxonomies for the list of items. And then figure out how I can let users check off their interest in these specific taxonomies and essentially subscribe them to posts related to only the taxonomies they are interested in.

    Am I off base in my thinking? Is there a better way to do what I am talking about?

    • Hi Sean – I know that the Subscribe2 plugin lets you do that type of thing for categories (which, of course, is a taxonomy type). I don’t know if there’s a plugin out there that will let you do it for custom taxonomies. Maybe there is.

      • I can probably write it, I am really just wondering if my thinking is sound. Using taxonomies as opposed to creating a directory of items with post types, which I could do, but if the items are really only the name, it seemed like taxonomies would be better, and more easily related to additional posts.

  2. Really helpful, thanks. This is a strong series, one of the best I’ve read, and I came across it at just the right time for the site I am working on!

  3. How do I display my custom fields in a series of posts displayed on a page. Im able to show ie numerous posts on a page but my custom field content doesnt display only the content inputed through the post or page editor

  4. Hi Joseph,

    I’m really a newbie in wordpress but i was able to do a custom type post with an associated custom taxonomy. Looking at your good example, i really apreciate if you help me with a light.
    Here:
    “ID, ‘location’, ‘Location: ‘, ‘, ‘, ” ); ?>”

    I wanna change the font size not only for “Location” but also for “Downtown”.
    As result all I can do is change the font size to Location:
    “ID, ‘location’, ‘Location: ‘, ‘, ‘, ” ); ?>”

    How to do for “Downtown”?

    Thank you,

    Camui

  5. Great tutorial on custom fields, posts and taxonomies. Thanks, it can really help people expand their uses for WordPress.

    Your readers may also like to know about another FREE plug-in called WP-Types (wp-types.com) that will allow creation of Custom fields, types and taxonomies, within the admin interface and link them all together to turn WordPress into a very powerful CMS.

    DISCLAIMER: I do work for this company and would like to refer newbies of WP-Types back to this tutorial. Nice work.

  6. Hi Joseph.
    i am a premium wpmu dev member with access to all your plugins. i am wondering why there was no mention of your own plugin CUSTOMPRESS in this series as it appears to handle all 3 elements including embed into the page. Can you tell me why it does not get a mention here please?

  7. Been all over to net, your descriptive piece on Custom Post Types, Custom Fields and Custom Taxonomy is simply outstanding. Great Work!
    One Question – If I use Custom Fields to insert data like Cast, Genre and Director (for a movie post), then how can I show these fields even on the Home Page, where every post gets listed (blog page, as some say)?

    • Thanks, Vajrasar.

      As for you question — I believe if you go into your index.php file and put the code in the proper place there, it will show up. However, many themes these days actually reference another file in the index.php file (maybe a file called content.php or something similar), and so if that’s the case then you’ll probably need to go into that file and put your code there, otherwise you will only get one custom field for the whole first page instead of different custom fields for each post.

        • Vajrasar – I’m not sure. Just try it out! If it doesn’t work, try other places as well. … You may be able to get help with this on the Genesis forum (I assume they have one). Different themes can be very different sometimes. But there’s no harm in trying it out first.

          • Thanks Joseph. I will try it out and will let you know if I succeed. Thanks again for such detail description.

  8. wow! these are amazing series. With so simple explanation you should become a writer ;).

    I was looking for some information how to set up a job board instead of using the boring themes and plugins.

    It was most difficult to understand the tags, categories et. and to see how I could used them best.

    Your articles really helped me to decide what to use for a employer, employee, branch, etc.

    Thanks, and a happy weekend from Holland!

  9. This was a helpful tutorial for getting started with a custom taxonomy, I appreciate your screenshots & annotated code. One note: you don’t cover anything about setting up an archive template to display the results when someone clicks on a custom taxonomy link.

    I’m working with a theme that doesn’t follow the standard template hierarchy, so after adding your nicely explained line of code to display my custom hierarchy, clicking on the linked term doesn’t display anything.

    Obviously you can’t support every theme, but it would help to note that some themes may have this issue and if someone wants to resolve it they’ll need to start by looking at how the theme normally handles archive type pages.

    • Hi – Thanks for the comment. As mentioned at the beginning of the series, this wasn’t really intended to be a technical tutorial. It’s really just an overview with a few examples to give a fuller picture.

      Also, for your particular situation, I’ve never really heard of a theme not following the default hierarchy. And I’m not sure why they would do this. Are you sure that’s the issue?

      http://codex.wordpress.org/Template_Hierarchy

      • The theme we’re using on this particular site is Presswork, which has a drag and drop layout that is implemented using a single index.php file that refers to a custom action.php file to load the content instead of using any of the basic files – it doesn’t have an archive.php, much less post.php or tag.php or category.php.

        This is a huge annoyance to customize and I’ll definitely be setting up some base criteria for selecting themes to avoid this situation in the future (I’m managing WP multisite installations at a university for various blogs & project websites).

        I just figured I’d note the issues I’ve found with custom taxonomies in case anyone else ran into similar results and wasn’t familiar with how the template hierarchy is supposed to solve this normally.

  10. I compleately broke my mind with this custom taxonomies.

    I have custom type ‘Recipe’ and hierarchical custom taxonomy ‘Food_type’
    Main Dish
    – Soup
    Side Dish
    – Rice
    – Vegetables
    etc.

    I plan to use Food_type as category. After choosing Side Dish should display all child post from Rice and Vegetables. But, all what I get it’s post in curent selected term.

    I’m using taxonomy-food_type.php template and code from archive.php

    Thank you in advance for help!

  11. Finally, finally, finally someone has explained the difference between taxonomies and categories in a way that makes sense to me. I could never figure out why we would use taxonomies, because to me it seemed they held the same information. For example, in building my game review site, I wanted to categorize games into genre, like mystery, steampunk, magical etc. but I couldn’t figure out if i should use categories or taxonomies. Now I know I want to use taxonomies because of the linking feature. So the difference isn’t in what data is stored in them, it’s how wordpress processes and displays the data. I never looked at it that way before. I kept thinking that somehow a taxonomy had to be something that held a “higher” piece of data, like headings of sections or something. You have no idea how much relief you have brought to my poor brain by finally getting this understanding through to it.

    Thanks!
    Kat

  12. Hi. Your link at the beginning of this article, “Part 2 is here: Custom Post Types”, which is supposed to point to part 2 is actually pointing to part 3.

  13. Firstly i would like to thank you for the explanation. I have a question that I wish you could help me with.

    I have a real estate theme with multiple custom taxonomies inside a custom post. So here is my situation.

    I have created a custom taxonomy directly in the php code inside the functions and they are.

    Type
    City
    Neighborhood
    Condominium

    What I have been trying to do for a while and have been researching for almost 2 weeks now is way to structure the hyperlink in a way that is SEO friendly. What I have now is:

    For city, neighborhood and condominium which are the most important:

    http://www.mysite.com.br/city
    http://www.mysite.com.br/neighborhood
    http://www.mysite.com.br/condominium

    What I need to do is this, for the neighborhood and condominium

    hyperlink for neighborhood – http://www.mysite.com.br/city/neighborhood
    hyperlink for condominium – http://www.mysite.com.br/city/neighborhood/conominium

    The current theme I have they are all one child of another, so it automatically displays the hyperlink in that format, but with custom taxonomy they are showing as independent.

    Is there any way I can get the city and be the base taxonomy for both neighborhood and condominium and the city and neighborhood to be the base for the condominium or am I wasting my time looking for an answer?

    Your help would be greatly appreciated.
    Thanks

    • Alexandre,

      If I understand correctly, basically you’re looking to put your custom posts into a category hierarchy — because they aren’t in one now. Is that right?

      This would be possible, I’m sure, but I’m not the person ask. Here’s what I would search for in Google (something like this): wordpress hierarchy categories custom post types

      I’m sure many people have asked this in places like the forums at wordpress.org and places like stackexchange.

      You might also look into breadcrumbs (after you get the above fixed): http://premium.wpmudev.org/blog/wordpress-breadcrumbs-seo-ux/

      • Hi Joe,

        Thanks for your reply! That is what I think my problem is, I am not googling the right words!

        Now that you gave me the right terms to search for, i might be able to get a solution for my problem. I was googling for “custom taxonomy hyperlink structure” or “how to register a child custom taxonomy” and both of them were taking me to the hyperlink structure of the post and not the custom taxonomy and all posts it has in it.

        I will try googling with your words and will check the link you sent me!

        Thanks so much for helping!

Participate