How to Create a New Taxonomy for a Custom Post Type

This post will focus on creating a new taxonomy for a custom post type (CPT).  A taxonomy is just a fancy way of saying category.  WordPress gives us the ability to create a new group of categories or a taxonomy for a given post type.  This provides us with the ability to categorize posts in ways that are specific to our needs.  An example would be if you had a CPT for a staff directory you might want a taxonomy for the departments.

Getting Started

This post builds off of what was started in the post How to Create a Custom Post Type in a Plugin. I will assume you have a working knowledge of creating plugins and/or have already gone through the aforementioned tutorial.

Step 1 Open the plugin PHP file

Open the my-cpt-plugin.php file located in wp-content\plugins.

Step 2 Add new taxonomy code

Add the following code just below the mycpt_create_member function in the my-cpt-plugin.php file. This code will create the new taxonomy.

add_action( 'init', 'mycpt_tx_create_member_category' );

function mycpt_tx_create_member_category() {
	register_taxonomy(
		'mycpt_tx_member_category',
		array('mycpt_member'),
		array(
			'labels' => array(
				'name' => _x('Member Categories', 'taxonomy general name'),
				'singular_name' => _x('Member Category', 'taxonomy singular name'),
				'search_items' => __('Search Member Categories'),
				'all_items' => __('All Member Categories'),
				'parent_item' => __('Parent Member Category'),
				'parent_item_colon' => __('Parent Member Category:'),
				'edit_item' => __('Edit Member Category'),
				'update_item' => __('Update Member Category'),
				'add_new_item' => __('Add New Member Category'),
				'new_item_name' => __('New Member Category Name'),
				'menu name' => __('Member Category')
			),
			'hierarchical' => true,
			'show_ui' => true,
			'query_var' => true,
			'rewrite' => array('slug' => 'mycpt-roster-category')
		)
	);
}

Instead of explaining each line I will just point out a few highlights. For more information about the various options refer to the WordPress Codex. I have found WordPress does a pretty good job of documenting their functions.

A couple of things to take note on in this code…

line 1  This is the action hook that tells WordPress to run your function right after WordPress initializes, but before everything else.
line 5  This is the name of the taxonomy as WordPress will know it.
line 6  This is the name of the post type for which the taxonomy is assigned. This can be any object type in WordPress (i.e. post, page, attachment, etc.)
line 8-19  These lines define the text that will be displayed at various points in the WordPress admin page for the taxonomy
line 21-24  These lines define different options about the taxonomy

I highly recommend checking out the WordPress Codex to explore the various available labels and options.

Step 3 Activate the plugin

If the plugin is already activated then the changes we have made should take place immediately. Refresh the WordPress admin page to see the new taxonomy. Otherwise, active the plugin. Once the plugin is activated and the WordPress admin page is refreshed you should see the new taxonomy displayed with the new custom post type.

Summary

In this tutorial you have created a custom taxonomy and assigned it to a custom post type. Remember, that even though in this tutorial we assigned the new taxonomy to a custom post type it can be assigned to just about any object type in WordPress. This is an elegant way to extend the functionality of WordPress for organizing your posts, pages and attachments and just about anything else you can think up!

I also wanted to mention again that this post builds off of what was started in the post How to Create a Custom Post Type in a Plugin.

Tagged with: , ,

Leave a Reply