Share this:

Technorati ReddIt Favorites

Using Custom Taxonomies

With WordPress 2.8 we got another interesting feature called custom taxonomies. You can make taxonomies from anything you want and use them much like you already use tags and categories. Unfortunately, themes offer only partial support (unsolvable problem) for custom taxonomies, but it’s not that complicated to implement.

When the first custom taxonomies were announced I wasn’t a big fan of the idea. Also, in the beginning custom taxonomies were very buggy, and hard to test. But, fortunately that’s now changed, and with 2 minor revisions it looks very stable. I have been interested in the way all that works, but I didn’t had a real chance to use custom taxonomies.

But now, I am working on new website, and using custom taxonomies came so natural and easy that I changed things a bit so I can use more of them for different types of data. Before I proceed on actual example on how to use it, I will try to explain exactly what you get bu using custom taxonomies.

As you know tags are widely used and very important on  any blog. But, tags are too wide and anything can be a tag. It’s not that specific as you might need. Same problem is with categories. In many cases you will need to add more specific, more targeted version of tags or even categories. Custom taxonomies can help you do just that. Let’s say that you want to make a website about movies. And you need to have a posts about movies. First thing you might wanna do is to make categories with movie genres. But that’s not that good solution when you have movies belonging to more than one genre. Also, you might wanna use categories for some more things. So, you create a custom taxonomy for genre. Similar will be with actors and directors. What’s the biggest benefit from this? If you have a lot of movies, you will have several movies directed by the same director. You can query all posts based on the director name and get all movies directed by him. Same goes for actors, and genres. Since archives template in theme already handles tags and categories, it will do just fine with custom taxonomies and you can easily get the archive page with post from a director or belonging to specific genre.

So, here is how to add these 3 custom taxonomies. You can do this from theme or plugin, all the same. If you use theme method, most logical location is functions.php.

add_action('init', 'init_taxonomies');

function init_taxonomies() {
register_taxonomy('genres', 'post', array('hierarchical' => false, 'label' => 'Genres', 'query_var' => true, 'rewrite' => true));
register_taxonomy('actors', 'post', array('hierarchical' => false, 'label' => 'Actors', 'query_var' => true, 'rewrite' => true));
register_taxonomy('directors', 'post', array('hierarchical' => false, 'label' => 'Directors', 'query_var' => true, 'rewrite' => true));
}
Taxonomies Widgets

Taxonomies Widgets

This will ensure that Post menu will be expanded to include all custom taxonomies you added, and that post edit page widgets for each one is added. You can see the two widgets on the image on the right. As you can see there are many parameters for adding taxonomy. First you give taxonomy short name (no spaces or special characters), and then you have array with settings. Hierarchical will be used to create new taxonomy to look like tags or like categories that support hierarchy. Label is the name used in menus or widgets. Query_var and Rewrite will add taxonomy to WP_Query object and to rewriter, and I suggest leaving that active.

Post menu will be expanded with new panel for each taxonomy, and this panel looks like tags or categories panel, and works the same.

Out of the box new archives URL’s should be working also, so you can use url like this one:

http://www.website.com/actors/ben-browder/

And you will get all posts that have actors taxonomy Ben Browder. This will get all movies that this actor starred in. Simple as that. The same way WP_Query gets new variable for each of the taxonomies you can also use, or to get tag cloud. Here are few examples:

get_the_term_list($post->ID, 'actors', 'Actors: ', ', ', '' );
wp_tag_cloud(array('taxonomy' => 'actors', 'number' => 20));
query_posts(array('actors' => 'ben-browder'));

First will get you list similar to tags list for the post in the theme, second render widget-like tag cloud, and the third one is example of query_posts functions, and will pull posts for Ben Browder.

And for the end of the post, here is the list of some useful URL’s:

http://codex.wordpress.org/Function_Reference/register_taxonomy
http://codex.wordpress.org/Function_Reference

The easiest way to work with taxonomies is to use our GD Taxonomies Tools plugin:

http://www.dev4press.com/plugins/gd-taxonomies-tools/

6 Responses to “Using Custom Taxonomies”

  1. Nickshor | September 13, 2010 at 16:36

    And how do I display related posts from taxonomy tags, automatic with an shortcode like [relatedtax]

    Is not really an option to build queries for every actor…

    GD Star Rating
    a WordPress rating system
    • MillaN | September 13, 2010 at 16:55

      There is nothing in WordPress to display related posts regardless of taxonomy. Related posts are usually built in theme or by the plugin. Depends on what you use.

      GD Star Rating
      a WordPress rating system
  2. Nickshor | September 13, 2010 at 17:21

    I use iStudio theme….
    Do you know any plugin that can do custom taxonomy and be able to display related post by taxonomy tags, dynamically…?
    Or maybe a script that make the taxonomy and the related post to put in the template…? I’m new to wp and php css…

    I really appreciate you taking your time with this!

    GD Star Rating
    a WordPress rating system
    • MillaN | September 13, 2010 at 17:46

      Next version of my GD Custom Posts and Taxonomies Tools plugin (only Pro version, 2.4 to be released by the end of September) will have elaborate function for getting related posts. My xScape themes implement this now partially.

      GD Star Rating
      a WordPress rating system
  3. Nickshor | September 13, 2010 at 18:01

    Ok, thanks… I’ll keep looking in the mean time…

    GD Star Rating
    a WordPress rating system

Leave a Reply

Follow us on Twitter

Social Networks

Subscribe via Feedburner Flickr Images Youtube Profile LinkedIn Profile
Gravity Forms
Hostgator
Get Clicky Web Analytics

Feedburner Feedburner updates

Sign up to receive news from this website to your email.

Dev4Press on YouTube

Archives