WordPress Custom Taxonomies

Custom taxonomies in WordPress can help you extend the standard ‘Categories’ and ‘Tags’ in your posts. They can also help extend WordPress custom post types if you have created one. Sometimes we run into situations where ‘Categories’ and ‘Tags’ just aren’t enough. Maybe we want to separate things out a little further. That’s what we are going to learn today.

What if you had a project where you were going to apply ‘Categories’ and ‘Tags’ to their posts but also something that would function similarly but called…oh I don’t know…Post Status. Let’s say these posts are being written by many people’s contributions and they need to show that a post is in the creative writing phase or in the proof reading stage. You would be able to harness custom taxonomies in WordPress to accomplish this. Once it has been created, it will show up underneath your ‘Categories’ and ‘Tags’ in the dashboard post menu.

Maybe we need another example. What if your client was a architect. They want to be able to write a blog post about drawing the floor plans and set a category of ‘Drawing Phase’. Then when they begin building the structure, they want to come back into that same blog post and write about the building process. So they would then change the category from ‘Drawing Phase’ over to ‘Building Phase’. Or if they had the taxonomy set as a tag then each phase could be added on as they go. This would require a custom taxonomy called ‘Phase’.

At the end of the article I will explain how to create custom taxonomies in WordPress for the standard post type. For now I will tell you about how to do it with a custom post type because that is where I worked with them in my recent project.

The Problem

In my last article I wrote about creating custom post types in WordPress. I recommend you read that article before reading anymore. In that article I mentioned a client who needed a separate post type to display animals that were up for adoption. They had a few custom taxonomies they needed such as ‘Cat Breeds’, ‘Dog Breeds’, an overall ‘Pet Category’, and a ‘Pet Status’ such as Adopted or In-Active.

The Solution

This is the code you will want to use to create custom taxonomies in WordPress:

// Register Custom Taxonomy
function custom_taxonomy() {

	$labels = array(
		'name'                       => _x( 'Taxonomies', 'Taxonomy General Name', 'text_domain' ),
		'singular_name'              => _x( 'Taxonomy', 'Taxonomy Singular Name', 'text_domain' ),
		'menu_name'                  => __( 'Taxonomy', 'text_domain' ),
		'all_items'                  => __( 'All Items', 'text_domain' ),
		'parent_item'                => __( 'Parent Item', 'text_domain' ),
		'parent_item_colon'          => __( 'Parent Item:', 'text_domain' ),
		'new_item_name'              => __( 'New Item Name', 'text_domain' ),
		'add_new_item'               => __( 'Add New Item', 'text_domain' ),
		'edit_item'                  => __( 'Edit Item', 'text_domain' ),
		'update_item'                => __( 'Update Item', 'text_domain' ),
		'separate_items_with_commas' => __( 'Separate items with commas', 'text_domain' ),
		'search_items'               => __( 'Search Items', 'text_domain' ),
		'add_or_remove_items'        => __( 'Add or remove items', 'text_domain' ),
		'choose_from_most_used'      => __( 'Choose from the most used items', 'text_domain' ),
		'not_found'                  => __( 'Not Found', 'text_domain' ),
	$args = array(
		'labels'                     => $labels,
		'hierarchical'               => false,
		'public'                     => true,
		'show_ui'                    => true,
		'show_admin_column'          => true,
		'show_in_nav_menus'          => true,
		'show_tagcloud'              => true,
	register_taxonomy( 'taxonomy', array( 'post' ), $args );


// Hook into the 'init' action
add_action( 'init', 'custom_taxonomy', 0 );

The structure of this code is very similar to creating a custom post type so if you don’t know what to put in these arrays go back and read my post about creating custom post types in WordPress. There is only two things that I want to point out. The first is in the register_taxonomy function you want to change the array( ‘post’ ) to array ( ‘your_custom_post_type’ ) so that WordPress knows that these custom taxonomies are linked to your custom post type and not the standard post type.

	register_taxonomy( 'taxonomy', array( 'custom_post_type' ), $args );

If you want to add this custom taxonomy to the standard post type then keep the array as array( ‘post’ ).

The second thing I want to point out is the hierarchical field. If set to false like the code below, your custom taxonomy will act like WordPress ‘Tags’ where you simply type in words and separate them with commas. Inversely, if you set it to true it will act like WordPress ‘Categories’ which gives you the ability to create parents and sub categories.

	$args = array(
		'labels'                     => $labels,
		'hierarchical'               => false,

You can copy and paste this code as many times as you need so that you can create as many taxonomies as your project requires. Knowing how to control and create these custom post types and custom taxonomies are essential. The more you familiarize yourself with the sometimes daunting functions of WordPress the better a developer you can become. I have been astonished at some of the things that come standard in WordPress that you don’t even know are back there. Go dig into it, get your hands dirty, break things, majorly break things because that is the only way you are going to know the ins and outs of WordPress. Or you can just follow my blog 😉