Custom WordPress Taxonomies

I attended the first ever Orlando WordCamp recently and I must say it was amazing. The entire core development team were there. Matt Mullenweg (co-founder) actually told me that this was the first time they were ever all in one place at the same time, some of them meeting for the first time ever! The day was full of great sessions and I’d like to impart a nice little trick I learned that may hopefully open up new concepts for your future WP developments.

We all know how to create categories for our posts. And we can all tag posts too. But how about making a custom taxonomy to greater organize these attributes? Instead of just dumping a bunch of keywords into the tags field, we can make a sort of ‘tag category’ if you will.

I’m going to cater to my audience and make a taxonomy about programming languages. So let’s say we have a post that uses both PHP and Mootools, we can tag the post this way.

Here is the base function we will use to do this…

 register_taxonomy($taxonomy,  $object_type,  $args);
  • The first value is the name of our taxonomy. In this case we will call it ‘language’.
  • The second refers to what this taxonomy will apply to. We’ll use ‘post’.
  • I won’t list all of the arguments here, but here are some key ones:
    • hierarchical: Set to ‘true’ for category like behavior, ‘false’ to use more like tags.
    • label: The name that will appear in our Admin Panel.
    • query_vars: We will set this to ‘true’ so we can call on this data in our template.
    • rewrite: Make our links prettier.

So here is my finished function:

register_taxonomy( 'language', 'post', array( 'hierarchical' => false, 'label' => 'Language', 'query_var' => true, 'rewrite' => true ) );

THAT’S IT!

WordPress will automatically build the admin menu’s for you (see screencap). Now we can assign tags like: ‘PHP’, ‘Mootools’, ‘jQuery’, and they will all be located into the ‘language’ taxonomy and not the general ‘Post Tags’.

So now that we have our custom taxonomy we can call it in our template with these functions

 wp_tag_cloud( array( 'taxonomy' => 'language', 'number' => 45 ) );  
//make tag cloud
 echo get_the_term_list( $post->ID, 'language', 'Language: ', ', ', '' );  
//print out the values for the post
 query_posts( array( 'language' => 'PHP', 'showposts' => 10 ) );  
//query the posts for a specified value in your custom taxonomy.

Therefore this is a useful tool to greater organize your posts and/or pages

Well, that’s it. Hope you’ve thought of a unique way to incorporate this into your site(s).

Check out Automattic’s P2 theme for a clever usage of this function.

For more info on custom taxonomies…

consult the codex at http://codex.wordpress.org/

One comment

  1. Yahzee says:

    Hi! Thank you for the tip, but I’m running into a problem. I have a movie website and I had created different custom types for each section inside a movie, like reviews, videos, etc.

    I got this code to query the posts with custom type “review” and to query only the ones with the taxonomy “movie” set to the current movie i.e. Avatar:

    $reviews = query_posts(array(
    ‘orderby’ => ‘date’,
    ‘post_type’ => ‘review’,
    ‘movie’ => ‘Avatar’
    ));

    I have two posts with the “movie” taxonomy value of “Avatar”, but nothing is showing. If a remove the:

    ‘movie’ => ‘Avatar’

    The query works fine, but retrieves all the review posts.

    Any idea on how to fix this?