Using custom taxonomies to create a movie database

![/user/media/2009/06/pop-critics-movies.png](Pop Critics Movie Database)

Lately, I’ve been writing a lot about how to create custom taxonomies in WordPress. I still get questions every day about practical applications with them.

Usually, I’m asked, “I understand the definition of taxonomy, but what do I do with them?” This is what I will attempt to answer in this post.

At the end of last week, I managed to talk my cousin into helping me set up a movie database. I thought this would be a great testing ground for custom taxonomies and allow me to present an example of how they can be used.

Go ahead and take a spin around the database and see what we’ve put together. I think it’s a neat concept.

You should make note that some of the things explained in this tutorial assume that you’re running WordPress 2.8+.

If something in the design is a bit wonky, pay it no attention. I just quickly merged it with the main site design and it could use some work.

The custom taxonomies

I created six new taxonomies for my WordPress install: actor, director, genre, producer, studio, and writer. These taxonomies allow readers to find movies based on certain criteria. For example, you can view movies that Tom Hanks has starred in.

The idea here is to give readers the ability to navigate around your site. You want organized content. You want content that’s linked together in meaningful ways. Custom taxonomies allow us to group our content in ways that simple categories and tags can’t.

Creating the custom taxonomies

I recently covered how you can easily create taxonomies in WordPress 2.8. To understand the mechanics behind creating and using them, read that post.

To do this, I added this code to my theme’s functions.php file:

<?php

add_action( 'init', 'create_pc_db_taxonomies', 0 );

function create_pc_db_taxonomies() {
	register_taxonomy( 'actor', 'post', array( 'hierarchical' => false, 'label' => __('Actors', 'series'), 'query_var' => 'actor', 'rewrite' => array( 'slug' => 'actors' ) ) );
	register_taxonomy( 'director', 'post', array( 'hierarchical' => false, 'label' => __('Directors', 'series'), 'query_var' => 'director', 'rewrite' => array( 'slug' => 'directors' ) ) );
	register_taxonomy( 'genre', 'post', array( 'hierarchical' => false, 'label' => __('Genres', 'series'), 'query_var' => 'genre', 'rewrite' => array( 'slug' => 'genres' ) ) );
	register_taxonomy( 'producer', 'post', array( 'hierarchical' => false, 'label' => __('Producers', 'series'), 'query_var' => 'producer', 'rewrite' => array( 'slug' => 'producers' ) ) );
	register_taxonomy( 'studio', 'post', array( 'hierarchical' => false, 'label' => __('Studios', 'series'), 'query_var' => 'studio', 'rewrite' => array( 'slug' => 'studios' ) ) );
	register_taxonomy( 'writer', 'post', array( 'hierarchical' => false, 'label' => __('Writers', 'series'), 'query_var' => 'writer', 'rewrite' => array( 'slug' => 'writers' ) ) );
}

?>

Creating term clouds with custom taxonomies

If you take a look around the movie database, you’ll notice I’ve made ample use of term (tag) clouds. Each represents a different taxonomy and will help you find movies in different ways.

![/user/media/2009/06/genres.png](A term cloud of movie genres)

Built into version 0.6 of the Hybrid theme (will be released after WordPress 2.8) is a widget called Tags. Traditionally, this widget would allow you to show a tag cloud. In the new version, there’s a select box to choose a taxonomy, which allows us to create a term cloud based on any custom taxonomy. Pretty cool, right?

If you’re not fortunate enough to be using the Hybrid theme, you can hardcode a term cloud like so:

<?php wp_tag_cloud( array( 'taxonomy' => 'taxonomy_name' ) ); ?>

Displaying custom taxonomies in a post

Also covered in my previous tutorial was how to list taxonomy terms for a post. Simply replacing taxonomy_name in the below code with the unique name of your taxonomy will handle that.

<?php echo get_the_term_list( $post->ID, 'taxonomy_name', 'Taxonomy Label: ', ', ', '' ); ?>?

Here’s a look at the movie page (single post view) of Turner & Hooch:

![/user/media/2009/06/turner-and-hooch.jpg](Custom taxonomies on a single-post view)

Notice how each taxonomy’s terms are listed. It gives you a view of the actors, genres, directors, producers, studios, and writers for the individual movie.

Displaying taxonomy terms in a page

When dealing with the vast amount of movies available, there’s no good way to show off everything in a sidebar and other small areas. I needed a way to show off each taxonomy on a separate page. So, I created six page templates to handle this:

In each template, I used the wp_tag_cloud() function (shown above) to show off a particular taxonomy. If you’re unfamiliar with creating page templates, read this tutorial on how to create your own.

Taxonomy term templates

Taxonomy terms get their own templates just like tags, categories, and other archives. In keeping with the Tom Hanks scenario, we’ll take a look at the Tom Hanks archive.

There are several things we have to do to make this happen. First, one useful bit of code I appended to my theme’s functions.php file allows me to add any XHTML to my term descriptions:

remove_filter( 'pre_term_description', 'wp_filter_kses' );

Once that was done, I found Tom Hanks under my Actors taxonomy in my WordPress admin (a sub-menu of Posts). I then added an image and short description of the actor. This shows up at the top of the Tom Hanks archive:

![/user/media/2009/06/turner-and-hooch.jpg](Custom taxonomies on a single-post view)

Not all themes’ archives are equipped to handle this. Instead of hacking up your theme’s archive.php template, copy and rename it to taxonomy.php. You’ll want to add these two code snippets to this file, replacing other code that might be in the way. Theme authors: You should take note of this.

To get the proper name of the taxonomy term (usually the archive page title), use this code:

<?php $term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) ); echo $term->name; ?>

To show the taxonomy term description, use this code:

<?php echo term_description( '', get_query_var( 'taxonomy' ) ); ?>

Try creating custom taxonomies for yourself

I hope this helps explain how taxonomies can be used on a site. I wanted to give everyone a real-world example to better understand the concept.

I didn’t go into complete detail on each bit of code because the post was getting much too long. In order to better understand how the code works, read over my tutorial on creating custom taxonomies. There’s a lot of great information in that post.

As always, feel free to ask questions and discuss. I’ll be happy to help out. Heck, go rate a few movies on the movie database. It could be fun!