Today we’ll go over “custom taxonomies.”
What Exactly does Taxonomy Mean?
Taxonomy is defined as “the branch of science concerned with classification.” So another way to put it is taxonomy means grouping things together – i.e. classifying them.
You’re no doubt already familiar with taxonomies in WordPress:
- Categories: Your categories are a type of taxonomy. You are grouping different posts together into categories.
- Tags: Tags are another type of taxonomy. When you create tags for your posts, you are creating groups of posts that are tied together with the tag you gave them.
How Categories and Tags Work on Your Site
I’m sure you know this information, but we’ll go over it here so you’ll have a point of reference fresh in mind for when we start defining WordPress custom taxonomies.
With both categories and tags, you can click on a link and see all the posts in that group. If you have a category called Pizza Restaurants, then you can access all those posts by navigating to the Pizza Restaurants category.
If you have a tag called “budget” (as in budget restaurants), then you can navigate to the tag page for “budget” and see all the posts that you tagged with that term.
Featured Plugin - WordPress Infinite SEO Plugin
And So What are Custom Taxonomies?
And so “custom taxonomies” work exactly the same way as categories and tags, except they’re “custom,” of course. You get to make them up from scratch. And like categories and tags, you can set them up to appear on your editing screen in either way: in a checkbox fashion with pre-determined terms like categories, or with an empty text box like tags that you can write whatever you like in.
Custom Taxonomies vs. Custom Fields
You may remember in Part 1 of this series, we talked about custom fields and how they are like extra boxes you put on your editing screen. And as you can see above, custom taxonomies also appear on your editing screen as extra boxes. And so because of that, it’s easy to confuse the two.
The simple difference between a custom taxonomy and a custom field is that with a taxonomy you are putting a post into a group that can be accessed as a group. In other words, it’s like putting a post into a special type of category. All the posts in that “category” (with that taxonomy) are linked together just as regular categories and tags link posts together.
The content published from a custom field is not linked together with other posts. It is simply displayed on the page.
So, again, in essence, a custom taxonomy that you make up yourself is really just a special type of category or a special type of tag. And, of course, you can have as many as you like.
Featured Plugin - WordPress Pop-Up Chat Plugin
Why Custom Taxonomies?
So if custom taxonomies are really just like categories and tags, why would you need them when you have categories and tags?
Well, some people don’t. For some, they can make do with categories and tags. But let’s take our hypothetical food-themed site from the previous two posts as an example again.
Even though I only have three different types of posts (restaurant reviews, recipes, nutrition posts), there are A LOT of different pieces of information in each.
If I tried to put everything that I wanted to group together in categories, I would have a large number of categories checked. For example, on one Pizza Restaurant post I might have Pizza, Downtown, Budget, and Walking Distance to Subway all checked in my categories box.
That’s not a problem, of course. I can make as many categories as I like; however, when I print the categories out on my post, they all get printed out together in one line because the code just says, “Get the categories.” And so that’s what it does, it gets every category checked and displays them in a line.
Featured Plugin - WordPress Newsletter Plugin
Decision Time: Custom Taxonomy or Custom Field
Another way to think of the difference between a custom taxonomy and a custom field is that a custom taxonomy usually gets printed on the page as a link (grouping together all the other posts with that taxonomy) while a custom field just gets printed on the page plain, without a link.
And so with this in mind, you will need to decide which types of information you want to be linked together in a group, and which types of information you want to just have printed out plain on the page.
In general, you might want to consider linking common terms that you repeat again and again together, and then leave specific things to be printed out without a link.
Choosing Taxonomy or Field: Some Examples
Going back to our restaurant review example, let’s take a look at a few of the different pieces of information we’re going to want to include, and then we’ll decide if we should use a custom taxonomy, a custom field, or both.
- Address & phone number – (custom field) This is obviously going to be unique to each restaurant, and so we don’t want to link this information together with other posts/restaurants. No other restaurant will have the exact same phone number and address. However, see the next section.
- Location – (custom taxonomy) Here we could use some general location indicators, such as Downtown, Northside, Southside, Eastside, Westside, etc. For this, it would be a good idea to use a custom taxonomy because then someone could click on the link for Downtown, for example, and see all the restaurants reviewed in the downtown area.
- Type of cuisine – (custom taxonomy) This might be a good one to use a custom taxonomy. You could have taxonomies such as Pizza, Seafood, Vegetarian, Chinese, Indian, Sandwiches, etc. Again, someone could click on Pizza and be taken to all the other pizza restaurant reviews.
- Price range – (maybe both) If you are giving exact prices, such as $7.95 – $24.95, then you will want to use custom fields. If you are giving general prices, such as Budget, Mid-range, Expensive, Very Expensive, then you will probably want to use custom taxonomies. That way, someone could click on Budget, for example, and see all the budget restaurant reviews.
Featured Plugin - WordPress Facebook Plugin
Important Technical Notes
When you set up a taxonomy, you will need to decide if it’s going to be hierarchical (meaning it can have sub groups) or if it will be non-hierarchical.
An easy way to think about this, again, is to think about categories and tags.
The category box is hierarchical. It can have sub-categories (children). But that also means that the choices in the box are set up beforehand. If you are only going to a few choices that you uses again and again, then you will probably want to go with hierarchical.
The tag box is non-hierarchical. With tags, of course, you typically have many more than you do categories.
And so when thinking about whether you want a certain taxonomy to be hierarchical or not, just ask yourself, “Would I make this a category or a tag?”
How to Create a Custom Taxonomy
Unless you’re a coder, as in the past two posts, I would suggest going with a plugin that will help you create your taxonomies. There are a number of them out there. Here are two.
More Taxonomies – This plugin (still in beta) is part of a suite of three plugins to deal with custom fields, custom post types, and custom taxonomies. The other two are the More Fields plugin and the More Types plugin.
Magic Fields 2 – I checked this plugin out recently, and it did the job nicely for me without any problems.
The settings on a custom taxonomy plugin will look something like the following. Notice how it lets me choose where which “post types” I want to place the custom taxonomy box in. In this case, I just chose my “review” type posts. So that taxonomy box shows up on my “Review Posts” screen but not on my regular post or page screens.
If you’d like to do it manually, you can see the WordPress Codex section for it.
Featured Plugin - WordPress Appointments Plugin
How to Display a Custom Taxonomy
As with custom fields, you need to insert code in your theme’s files where you want your custom taxonomy content to appear.
Here’s the way I did for the example I used in this post. Here it is again.
This piece of code is for the first line, the “location” line.
<?php echo get_the_term_list( $post->ID, 'location', 'Location: ', ', ', '' ); ?>
You’ll notice that I included some text before the content – I included Location: and then printed the content (in this case it was “Downtown”). If you wanted something else for your text, you would just change my wording (the Location with a capital “L” and the colon.). If you just wanted to print the content without any text before it, then you would just delete that word.
So that’s some basic code you can use. But in my example, you’ll notice that my text before the content is in bold. So actually my real piece of code looks like this (with <strong> around my text).
<?php echo get_the_term_list( $post->ID, 'location', '<strong>Location:</strong> ', ', ', '' ); ?>
And my entire section of code looks like this …
<?php echo get_the_term_list( $post->ID, 'location', '<strong>Location:</strong> ', ', ', '' ); ?>
<?php echo get_the_term_list( $post->ID, 'price', '<strong>Price:</strong> ', ', ', '' ); ?>
<?php echo get_the_term_list( $post->ID, 'cuisine', '<strong>Cuisine:</strong> ', ', ', '' ); ?>
<?php echo get_the_term_list( $post->ID, 'subway', '<strong>Subway:</strong> ', ', ', '' ); ?>
Featured Plugin - WordPress Google Maps Plugin
Using Other Plugins
Once you get your taxonomies set up, there are other types of plugins you can get to display them. Here’s an example of a custom taxonomy widget plugin I tried.
In this case, I decided to show the different locations of the restaurants. This was pulled from my newly created “location” taxonomy.
If you’ve gone through all three parts of this series, hopefully you have a better understanding of how these three confusing terms – custom fields, custom post types, and custom taxonomies – are different, and yet how they can work hand in hand.
And even more importantly, hopefully these posts have given you some ideas for you own site.
Featured Plugin - WordPress Q&A Site Plugin
Photo: Arrows on grow from BigStock