How to Create Custom Taxonomies in WordPress

Many people think of WordPress as a blogging tool, mainly because it has posts, categories, tags, etc. What most people don’t know is that all posts, categories, tags, can be replaced by custom post types and custom taxonomies. In this article we will show you how to create custom taxonomies in WordPress as well as how to display custom taxonomies in your WordPress theme.

What is a Taxonomy?

Taxonomy in WordPress is one of those things that everyone use, but they do not know that they are using it. Derived from the biological classification method Linnaean taxonomy, WordPress taxonomies are used as a way to group posts and custom post types together. WordPress has two very popular taxonomies that people use on a regular basis: Categories and Tags (Read: Categories vs. Tags: Best Practices). You can use custom taxonomies to create custom groups and bring them under one umbrella. For example, you have a custom post type called Books. Even though you can use categories, you may not want to mix the two because they are used differently. You can register a new custom taxonomy called Topics. You can add topic terms like: Adventure, Romance, Non-Fiction, etc. This would allow you and your users to sort your books by each topic. Taxonomies can also be hierarchical meaning that you can have main topics like: Fiction, Non-Fiction, and Children. Then have subtopics under each category for example fiction would have thrillers as a sub-topic.

Now that you know what is a custom taxonomy, let’s learn how to create custom taxonomies in WordPress. We will use two methods to create custom taxonomies. Method 1 would utilize a plugin for those who choose not to deal with code. Method 2 on the other hand would be the code method for those who prefer to do everything without a plugin.

Creating Custom Taxonomies – The Easier Way

Let’s start creating a custom taxonomy. First, you need to install and activate Simple Taxonomy WordPress plugin. Go to Settings » Custom Taxonomies to create a new taxonomy:

Creating a custom taxonomy in WordPress

The first part of creating a custom taxonomy is giving it a name, which needs to be all lowercase and no weird characters. The second option is whether or not this taxonomy will be hierarchical. If you want to create a taxonomy like categories where you can add a parent and child term then choose True, other wise choose false if you want terms to be added like tags.

Third option is to associate this taxonomy with a post type and last option is whether or not you want to add terms automatically, choose none.

But we are not done yet. Lets assume that you are creating a taxonomy and calling it Topics. Now you need to tell WordPress how it should translate user interface for the topics.

Translating taxonomy for UI

After providing translations for the UI, press the Add Taxonomy button. Once a custom taxonomy is created, it will appear under Posts and will have a similar interface like Categories or Tags. Also the custom taxonomy field will also appear in post edit area.

Custom taxonomy in Post Edit area

Manually Creating Custom Taxonomies

Add the following code in your theme’s functions.php file or in a site-specific plugin (recommended) to create a hierarchical custom taxonomy like categories:

01 //hook into the init action and call create_book_taxonomies when it fires
02 add_action( 'init', 'create_topics_hierarchical_taxonomy', 0 );
04 //create a custom taxonomy name it topics for your posts
06 function create_topics_hierarchical_taxonomy() {
08 // Add new taxonomy, make it hierarchical like categories
09 //first do the translations part for GUI
11   $labels = array(
12     'name' => _x( 'Topics', 'taxonomy general name' ),
13     'singular_name' => _x( 'Topic', 'taxonomy singular name' ),
14     'search_items' =>  __( 'Search Topics' ),
15     'all_items' => __( 'All Topics' ),
16     'parent_item' => __( 'Parent Topic' ),
17     'parent_item_colon' => __( 'Parent Topic:' ),
18     'edit_item' => __( 'Edit Topic' ),
19     'update_item' => __( 'Update Topic' ),
20     'add_new_item' => __( 'Add New Topic' ),
21     'new_item_name' => __( 'New Topic Name' ),
22     'menu_name' => __( 'Topics' ),
23   );   
25 // Now register the taxonomy
27   register_taxonomy('topics',array('post'), array(
28     'hierarchical' => true,
29     'labels' => $labels,
30     'show_ui' => true,
31     'show_admin_column' => true,
32     'query_var' => true,
33     'rewrite' => array( 'slug' => 'topic' ),
34   ));
36 }

To create a non-hierarchical custom taxonomy like Tags, add this code in your theme’s functions.php or in a site-specific plugin:

01 //hook into the init action and call create_topics_nonhierarchical_taxonomy when it fires
03 add_action( 'init', 'create_topics_nonhierarchical_taxonomy', 0 );
05 function create_topics_nonhierarchical_taxonomy() {
07 // Labels part for the GUI
09   $labels = array(
10     'name' => _x( 'Topics', 'taxonomy general name' ),
11     'singular_name' => _x( 'Topic', 'taxonomy singular name' ),
12     'search_items' =>  __( 'Search Topics' ),
13     'popular_items' => __( 'Popular Topics' ),
14     'all_items' => __( 'All Topics' ),
15     'parent_item' => null,
16     'parent_item_colon' => null,
17     'edit_item' => __( 'Edit Topic' ),
18     'update_item' => __( 'Update Topic' ),
19     'add_new_item' => __( 'Add New Topic' ),
20     'new_item_name' => __( 'New Topic Name' ),
21     'separate_items_with_commas' => __( 'Separate topics with commas' ),
22     'add_or_remove_items' => __( 'Add or remove topics' ),
23     'choose_from_most_used' => __( 'Choose from the most used topics' ),
24     'menu_name' => __( 'Topics' ),
25   );
27 // Now register the non-hierarchical taxonomy like tag
29   register_taxonomy('topics','post',array(
30     'hierarchical' => false,
31     'labels' => $labels,
32     'show_ui' => true,
33     'show_admin_column' => true,
34     'update_count_callback' => '_update_post_term_count',
35     'query_var' => true,
36     'rewrite' => array( 'slug' => 'topic' ),
37   ));
38 }

Notice the difference between two codes. Value for hierarchical argument is true for category-like taxonomy and false for tags-like taxonomies. Also in the labels array for non-hierarchical tags-like taxonomy, we have added null for parent_item and parent_item_colon arguments which means that nothing will be shown in the UI to create parent item.

Displaying Custom Taxonomies

Here is how you can display the terms you added to a custom taxonomy on your single post page. Add this single line of code in your single.php file within the loop:

1 <?php the_terms( $post->ID, 'topics', 'Topics: ', ', ', ' ' ); ?>

You can add it in other files as well such as archive.php, index.php, and anywhere else you want to display the taxonomy.

By default your custom taxonomies use the archive.php template to display posts. However, you can create a custom archive display for them by creating taxonomy-{taxonomy-slug}.php.

Custom taxonomies can be used in many ways. Combine them with custom post types and custom meta boxes, and you can create highly customized content management system (CMS) built to meet your needs. Let us know how you are using custom taxonomies on your websites?


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s