The Ultimate Guide to WordPress Taxonomies

What is a Taxonomy?

Taxonomy is one of those words that most people never hear or use. Basically, a taxonomy is a way to group things together. In WordPress, a “taxonomy” is a grouping mechanism for some posts (or links or custom post types). WordPress 3 has introduced the ability to create custom hierarchical taxonomies, and due also to the addition of Custom Post Types the usefulness of custom taxonomies has increased.

WordPress has four built in taxonomies that you’ve probably used already. We will be skipping the ‘link category’ and ‘nav menu’ taxonomy and looking at the other two which are more commonly used:

  • Category
    The ‘category’ taxonomy lets you group posts together by sorting them into various categories. These categories can then be seen on the site by using /category/name types of URLs. Categories tend to be pre-defined and broad ranging.
  • Tag (added in WP 2.3)
    The ‘post_tag’ taxonomy is similar to categories, but more freeform. Tags can be made up on the fly, by simply typing them in. They can be seen on the site in the /tag/name types of URLs. Posts tend to have numerous tags, and they are generally displayed near posts or in the form of tag clouds. Tags are also one-dimensional, not allowing any hierarchical structure.

WordPress 2.3 also introduced the ability to create your own custom taxonomies, although this arguably only became popular with the advent of 3.0, especially due to their ability to be used in conjunction with Custom Post Types. Therefore, taking a look at this new feature, we can define it as follows:

  • Custom Taxonomy
    A custom taxonomy is a a method for creating a custom way of grouping content. We can therefore extend beyond tags and categories and create our very own organizational system for the data on our site.

Creating a Custom Taxonomy

You probably all know how you can create tags and categories, what we’re really interested is custom taxonomies, so that’s what I’ll be focusing on here. In case you are not feeling too confident about tags and categories, do brush up on your knowledge here before getting into custom taxonomies.

Custom taxonomies can either be single-level or hierarchical. The two screenshots below (from this excellent nettuts tutorial), show the difference:

Single Level Taxonomy

Hierarchical Taxonomy

Really, the best way to understand what a custom taxonomy is and how it can be useful is to create one from scratch.

In our example, we will be dealing with recipes. Our client would like to be able to add ingredient information to each of his recipes. He would also like to classify recipes by country. Sounds like a good case for implementing custom taxonomies! Let’s get to work. First, we created a custom post type called ‘Recipes’. At this point, we need to create the custom taxonomy to house the ingredients and countries.

Countries will be predefined, the client know that he will be publishing recipes from a specific number of countries. There might also be the possibility of dividing each country by region, so the hierarchy needed in that case would be similar to the native WordPress categories. As such, we will be implementing countries as a hierarchical custom taxonomy.

Ingredients will be added on the fly, and can also be thought of as micro-data about each recipe. As such, they would probably be most suited to single-level taxonomies (similar to post tags).

Add the following code in your theme’s functions.php file:

The end result from this code will be the creation of two new items called “Ingredients” and “Countries” under your “Recipes” menu item in the admin.

Let’s take a minute to understand what we’ve done in the code above. The register_my_taxonomies() function is a container for all our new taxonomies, which are then created using the register_taxonomy() function.

This is the definition of the register_taxonomy() function:

$taxonomy: name of the taxonomy

$object_type: which object types (posts, pages or custom post types) will be able to use this taxonomy

$args: a set of arguments (full explanation on Codex)

Taking a look at the arguments we have used:

‘hierarchical’: defines whether the taxonomy will be single level (false) or hierarchical (true)

‘public’: defines whether the taxonomy should be exposed in the admin UI

‘query_var’: If ‘true,’ we’ll be able to ask WordPress for posts dependent upon the selections for this taxonomy. For example, we could search for all the recipes where the country taxonomy has ‘Spain’ selected.

‘labels’: information about the way the taxonomy information is going to be printed in the WordPress admin.

Listing a Post’s Taxonomy Terms

Now on each recipe post we want to display the ingredients associated with that recipe. That’s easy, just plug in the following code into the template used by your recipe post type.

Alternatively you can use the_terms() function which is the same but echoes the results.

Show Posts from a Specific Taxonomy

Show the latest 10 posts which have eggs (from our custom taxonomy) as one of their ingredients.

Taxonomy Conditionals

Conditional tags check a specific condition and return true if the condition is met or false if the condition is not met. We have some special conditionals for taxonomies:

When any Taxonomy archive page is being displayed.

When a Taxonomy archive page for the flavor taxonomy is being displayed.

When the archive page for the flavor taxonomy with the slug of ‘mild’ is being displayed.

Returns true when the flavor taxonomy archive being displayed has a slug of either “sharp”, “mild”, or “extreme”.

When a particular taxonomy is registered via register_taxonomy(). Formerly is_taxonomy(), which was deprecated in WP 3.0

Plugins for Custom Taxonomies

A list of plugins that can be used for custom taxonomies building, display and further customisation, partially taken from Michael’s blog post.

More Taxonomies
Allows you to add custom taxonomies via an interface in the WordPress admin. You can also set editing capabilities based on user roles and list taxonomy items in specific menus.
Download

Taxonomy Manager
Manage taxonomies from WordPress back-end. Add/Edit/Delete taxonomies and more!
Download

GD Custom Posts and Taxonomy Tools
Enables you to create and manage your own custom taxonomies.
Download

Taxonomy Terms List
Automatically create multiple lists of taxonomy terms associated a given post in the single post view. This one is plug and play, just activate and you’re good to go.
Download

Taxonomy Drill down
This plugin lets you do faceted search using multiple custom taxonomies. It has a drill-down widget with multiple display modes.
Download

Taxonomy List Shortcode
Add a shortcode to your WordPress powered site enabling you to create a styled list of terms for any given taxonomy. Useful for creating custom index pages for your taxonomy structures.
Download

Taxonomy Images
Trick out your categories, tags and custom taxonomies! This plug-in enables you to link images from your media library to every term of every taxonomy on your WordPress installation. This plug-in also includes a shortcode enabling you to make stylized index page of your taxonomy terms.
Download

Simple Taxonomies
Another plug-in that enables you to create and edit custom taxonomies. Simple Taxonomies also enables you display taxonomies in a few different places throughout your blog.
Download

Some Scenarios for Custom Taxonomies

In a very good post about Custom Taxonomies, Justin Tadlock offers a few scenarios in which custom taxonomies could be useful:

  • Finding actors (taxonomy) that have played in specific movies (content).
  • Listing musicians (taxonomy) that have performed on various albums (content).
  • Locating restaurants (content) that serve specific types of food (taxonomy).
  • Getting recipes (content) that use certain ingredients (taxonomy).
  • Searching for jobs (content) in different fields (taxonomy).
  • Visiting a forum (taxonomy) that lists forum topics (content).
  • Scanning for books (content) written by your favorite author (taxonomy).

Other Uses for Custom Taxonomies

In this article we have discussed the creation of custom taxonomies for custom post types, but they can also be created for other WordPress objects such as Users. We will tackle this use in a future tutorial.

Excellent Posts Comparing Categories and Tags

http://lorelle.wordpress.com/2005/09/09/categories-versus-tags-whats-the-difference-and-which-one/

http://lorelle.wordpress.com/2005/09/13/categories-versus-tags-defining-the-limitations/

If you enjoyed this post, make sure to subscribe to our rss feed.

About Jean Galea

Jean Galea is a WordPress developer, podcaster and entrepreneur. He is the founder of WP Mayor and WP RSS Aggregator. His personal blog can be found at jeangalea.com. Check out his podcast focused on doing business with WordPress over at Mastermind.fm.

13 Responses

  1. Mark "Chief Alchemist" Simchock

    Nice overview Jean.

    The biggest problem I found with taxonomies is/was documentation. Yes, WP supports them but if you want to anything outside the couple of standard WP functions there’s quite a learning curve.

    If you don’t mind, I’d like to share a couple functions that came out of that learning curve:

    http://www.chiefalchemist.com/customize-wordpress-taxonomy-functions-the-remix/

    And yes, the Chief Alchemist site makes use of taxonomies 🙂

    Let me know what you think.

    1. Jean
      Jean May 30, 2011 at 10:56 | | Reply

      Mark, I suggest using the SyntaxHilighter plugin on your site, it would make the code more readable.

  2. Mark "Chief Alchemist" Simchock

    Hey Jean.

    Thank you. I’ll check that plugin out. I should have known there was something somewhere to make the more presentable. Silly me 🙂

    Let me know what you think of the functions. I write PHP but I more dabble than I’m a coders’ coder. If it works and looks reasonably clean that’s my sweet spot. Tips, tricks, feedback, etc. are always welcome.

  3. Mark "Chief Alchemist" Simchock

    Hello again Jean. Which do you use? I did a search on WP.org and got too many 🙂 Maybe you can help save me some time? Maybe that’s an article? A review of plugins for posting code? 🙂

    1. Jean
      Jean June 1, 2011 at 17:05 | | Reply

      Try this:

      http://wordpress.org/extend/plugins/syntaxhighlighter/

      I need to write a post about syntax beautifiers one of these days.

  4. Mark "Chief Alchemist" Simchock

    Cheers mate. I’m on it.

  5. Ultimate Guide Wordpress
    Ultimate Guide Wordpress June 10, 2011 at 06:07 |

    […] The Ultimate Guide to WordPress Taxonomies – WPMayor Difference between WordPress categories, tags and custom taxonomies. We also go through the step-by-step creation of a custom taxonomy. […]

  6. korkis
    korkis August 20, 2011 at 13:31 | | Reply

    Hi guys…

    Im using video theme http://templatic.com/freethemes/video

    any idea how to remove taxonomy category?

    .com/videosctegory/…… to .com/…..
    .com/videos/…… to .com/…….

    thanks .

  7. Working with Taxonomies | Daniel Sliwka design
    Working with Taxonomies | Daniel Sliwka design April 2, 2012 at 18:19 |
  8. Lizzie Wortham
    Lizzie Wortham October 19, 2012 at 16:06 | | Reply

    In your example about recipes, how would you add the amount of the ingredients into the recipes? Is there a way to connect an ingredient from a taxonomy but then add the amount in the recipe post?

    Thanks,
    Lizzie

  9. houfton
    houfton February 5, 2016 at 03:43 | | Reply

    I use the Types plugin which enables you to easily add custom post types, taxonomies and custom fields. It can also be used to modify existing post types. It has some powerful paid-for sister plugins which I also use but Types itself is free.

    https://wordpress.org/plugins/types/

Leave a Reply

  
Please enter an e-mail address