Using WordPress taxonomies

So today I thought I’d share a few web design tips I’ve picked up whilst using WordPress when it comes to custom Taxonomies. The main scenario when these are going to be used is when you’re using WordPress as a CMS. Now, I’m not going to go into the whole building a CMS process, that’s a story for another day. Today I’m just going to look at some of the benefits of using these techniques and what scenarios how you can set them up correctly to use.

I’m going to assume that you already know what custom post types and taxonomies are. For those of you who don’t, you can read about custom post types here and taxonomies here.

There’s only 2 examples I want to share for now, but both could prove to be very useful – they have been for me!

Example 1 – Custom Taxonomies

So the first thing I want to go through is using custom taxonomies. I didn’t realise how useful taxonomies could be until I started using them some time ago and they haven’t let me down since. You can create your own taxonomies in your theme’s functions.php and assign this to an existing content type, or a new one. One of the main options you get is whether the taxomony is hierarchical or not. This will be the difference between a ‘category’ structure, or a ‘tag’ structure. I rarely use tags when developing a CMS, so almost always opt for a hierarchical value of true. So that means you can categorise your posts/content even further. When using CPT’s, you might want to categorise the posts inside and although custom fields gives a solution, I think taxonomies are the best for this purpose.

There’s two main points which I’ve found, that make these so good. The first one being this.

Imagine you create a new post type. You can do this using a plugin or manual functions.php upload. For this example, let’s say we are using a plugin (I normally use CMS Press). Next we create a taxonomy called ‘location’. Within that, we add cities in the UK as options (hierarchical set to true so it’s a category structure). So imagine we are posting events happening throughout the UK and using our taxonomy to categories the posts by location. On the front-end of the website, we can use a simple query to only show posts from a specific area.

$location = 'variable'; // set to a custom value
$args = array( 'post_type' => 'NEW POST TYPE', 'posts_per_page' => 5, 'location' => $location); // query new post type & location
 $loop = new WP_Query( $args );

So it’s as easy that really. You can do the same sort of thing with custom fields as I said, but I haven’t found a way that is any better than this.

Example 2 – Taxonomies in the URL

The second example is linked with the first. Imagine if we want to categorise our events but we want this to stick in the URL. So the URL would go from this:

  • http://www.domain.co.uk/post_type/single_event

to

  • http://www.domain.co.uk/post_type/location/single_event

Now, this is the ideal situation and it’s not that hard to achieve. Luckily, I found a solution which took some time, but if you’re reading this you won’t get into the same situation. The first hing you need to do is register your custom post type through your functions.php, not using a plugin! This is key. Plugins do not support URL taxonomy rewrites (the ones I know of anyway). So once you’ve got your CPT registered through your function.php you can register your custom taxonomy using the same method. Remember, set hierarchical to true!