Shelley Bassett



Adding a custom taxonomy to a post type

May 7, 2018

Using a custom taxonomy is a really easy way to separate posts within a post type. Custom taxonomies have numerous benefits, added flexibility, and are easy to implement.

Creating a custom taxonomy provides an easy way to separate posts without cluttering up the existing “category” or “tag” taxonomies. Designed specially for use with WordPress hooks, these taxonomies should be reserved for “posts” . Custom taxonomies can be attached to either the sites posts, or a custom post type and can also be used across multiple post types without causing conflicts.Custom taxonomies should be added via a plugin to allow continued functionality on a theme change, but can also just be added in the themes functions.php  file. The WordPress codex also contains helpful and in-depth information on custom taxonomies.

Adding a Custom Taxonomy

The php for a custom taxonomy is fairly straightforward. It’s mostly making sure that the names and labels are correct. Make sure the hook is called in a function on 'init' . This will ensure your taxonomy always exists when you want to use it.

The most important part of this code is the line 'hierarchical' => true, . This determines whether the taxonomy is grouped and will contain subcategories, or is just a list of terms like tags. To achieve tag-like functionality, use 'hierarchical' => false,  instead.

Terms in the 'rewrite' => array(),  are also important to the taxonomies functionality. This array determines determines the url for the archives of the custom taxonomy. In this example, the posts in the new taxonomy are found at If you have a hierarchical taxonomy and wish to include all the sub-categories in the url, add 'hierarchical' => true, into the array.

Don’t forget to change the “post-type” to the post type that you want to attach the taxonomy to. To add the custom taxonomy to a standard blog post, use register_taxonomy('taxonomy', 'post', $args); . This simple WordPress function allows much greater flexibility in categorising posts. Custom post types are in action all over my site, and are most noticeable in the portfolio. Those categories? All part of a custom taxonomy attached to the custom post type.

Creating a Taxonomy within a Custom post Type

If linking taxonomies and custom post types is giving you grief, it may be easier to create the taxonomy at the same time as the post type. The following code shows both a custom post type and a custom taxonomy. It also builds on the custom post type tutorial found here.

This code is combining the custom taxonomy and the custom post type into one single function. They are linked to each other, and the post permalink (with the category slug “products”) containing the full heirachy. The largest difference between this function and original post type function is the name of the archive category. Be sure to set the archive to the new one, otherwise WordPress will use the default categories.

Creating a linking taxonomies is easy. To avoid unwanted interaction with other plugins, be sure to include an identifier with your function and the id. This also helps to identify your post types when called on other pages.