The Ultimate Guide to WordPress Taxonomies

If you purchase through a link on our site, we may earn a commission. Learn more.

In this post we examine the difference between WordPress categories, tags and custom taxonomies. We also go through the step-by-step creation of a custom taxonomy.
Table of Contents
WP Engine High Performance Hosting
BionicWP Hosting

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:

[php]
add_action( ‘init’, ‘register_my_taxonomies’, 0 );

function register_my_taxonomies() {

// register taxonomy to hold our ingredients
register_taxonomy(
‘ingredient’,
array( ‘recipe’ ),
array(
‘hierarchical’ => false,
‘public’ => true,
‘query_var’ => true,
‘rewrite’ => true,
‘labels’ => array(
‘name’ => __( ‘Ingredients’ ),
‘singular_name’ => __( ‘Ingredient’ )
),
)
);

// register taxonomy to hold our countries
register_taxonomy(
‘country’,
array( ‘recipe’ ),
array(
‘hierarchical’ => true,
‘public’ => true,
‘query_var’ => true,
‘rewrite’ => true,
‘labels’ => array(
‘name’ => __( ‘Countries’ ),
‘singular_name’ => __( ‘Country’ )
),
)
);

}
[/php]

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:

[php]register_taxonomy($taxonomy, $object_type, $args);[/php]

$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.

[php]
echo get_the_term_list( $post->ID, ‘ingredient’, ‘Ingredients: ‘, ‘, ‘, ” );
[/php]

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.

[php]query_posts( array( ‘ingredient’ => ‘eggs’, ‘showposts’ => 10 ) );[/php]

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:

[php]is_tax()[/php]

When any Taxonomy archive page is being displayed.

[php]is_tax( ‘flavor’ )[/php]

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

[php]is_tax( ‘flavor’, ‘mild’)[/php]

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

[php]is_tax( ‘flavor’, array( ‘sharp’, ‘mild’, ‘extreme’ ) )[/php]

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

[php]taxonomy_exists()[/php]

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.

Jean Galea

Jean Galea is an investor, entrepreneur, and blogger. He is the founder of WP Mayor, the plugins WP RSS Aggregator and Spotlight, as well as the Mastermind.fm podcast. His personal blog can be found at jeangalea.com.

Discover more from our archives ↓

Popular articles ↓

16 Responses

  1. Pingback: How To Create A Custom WordPress Document Library For Your Site - WP Mayor
  2. Pingback: How To Automatically Generate Affiliate Income From A Curated Content Site
  3. Pingback: Oxygen 2.0 Review: A True Visual WordPress Website Builder - WP Mayor
  4. Pingback: How To Create A Flexible WordPress Video Gallery With Posts Table Pro - Pixallus
  5. 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.

  6. 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

  7. Hi guys…

    Im using video theme

    any idea how to remove taxonomy category?

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

    thanks .

  8. Pingback: Ultimate Guide Wordpress
  9. 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? 🙂

  10. 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.

  11. Thanks for sharing Mark, will definitely be checking these functions out next time I use custom taxonomies.

  12. 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:

    [Redacted]

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

    Let me know what you think.

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

Share Your Thoughts

Your email address will not be published. Required fields are marked *

Claim Your Free Website Tip 👇

Leave your name, email and website URL below to receive one actionable improvement tip tailored for your website within the next 24 hours.

"They identified areas for improvement that we had not previously considered." - Elliot

By providing your information, you'll also be subscribing to our weekly newsletter packed with exclusive content and insights. You can unsubscribe at any time with just one click.