How To: Add Taxonomies to Your Custom Post Types in WordPress

Custom Post Types are not new to WordPress, they’ve been around since version 3.0 and have really changed the “WordPress as a CMS” game ever since. Custom taxonomies though, have been around even earlier and are used to group your posts and custom post types.

This post will give you a short insight on taxonomies in WordPress and show you how to add different taxonomies to different post types without having to alter any of the core or third party plugins and themes code.

By default WordPress comes with five registered taxonomies: categories, tags, post formats, link categories and navigation menus, and not all of them are as transparent as tags and categories are. It’s no secret that you can create your own taxonomies in WordPress with register_taxonomy and add the new taxonomy to the post types you like using its second argument.

Suppose you have a custom post type called game and you’d like to classify them into platforms using a custom taxonomy. Here’s how you would register the taxonomy and add it to the game post type:

register_taxonomy( 'platform', 'game', $args );

The third argument is the taxonomy arguments, very well explained here (don’t forget that there are reserved terms too.) The second argument makes sure that the new platform taxonomy is registered with the game post type. If you’d like to register it for more than one post type, you’ll have to use an array, like this:

register_taxonomy( 'platform', array( 'game', 'post' ), $args );

This will make sure the taxonomy is available to both games and regular posts. Using this technique is good if you have control over the taxonomy, but sometimes a different plugin will register the taxonomy for you and you’ll want to add the taxonomy to your own post type. How would you do that?

Well the simplest method is to edit that plugin’s source code, but it would break when the plugin is updated. Then again, what if we need to add the core taxonomy (categories or tags) to my custom post types? We certainly don’t want to edit the WordPress core code, right?

Luckily there’s a function called register_taxonomy_for_object_type which adds an already registered taxonomy to a custom object type. Suppose we need to add the same categories and tags we use with posts to our new games post type. Here’s how we do it:

register_taxonomy_for_object_type( 'category', 'game' );
register_taxonomy_for_object_type( 'post_tag', 'game' );

How about adding the platform taxonomy to our posts, but outside of the taxonomy registration code? Here’s the answer:

register_taxonomy_for_object_type( 'platform', 'post' );

So yeah, it’s all quite simple as you can see, so if you’ve authored a custom taxonomy plugin, don’t go register it with every available post type. It’s much easier to add them later on than to remove them.

Speaking of removing them, here’s a “hack” of how you would remove the category taxonomy from the post post type using the $wp_taxonomies global:

add_action( 'init', 'my_taxonomy_cleanup', 11 );
function my_taxonomy_cleanup() {
	global $wp_taxonomies;
	$wp_taxonomies['category']->object_type = array_diff( $wp_taxonomies['category']->object_type, array( 'post' ) );
}

Not very clean I agree but it will work. Note the action registration with the priority of 11 (which is quite late.) If you’d like to remove a taxonomy from an object type, you’ll first have to find out at what stage was the taxonomy added to the post type. In the above case, the default taxonomies are added during init with priority 0 (highest) so init with priority of 11 will work just fine.

Well that’s enough for today folks, hope you enjoyed it and learned something new. Feel free to ask questions using the comments section below (if still open) or ping me on Twitter. Cheers!