11th September, 2015 | Tutorials |

How to organize your WordPress site using Custom Taxonomies

Using Wordpress? Get our lightening fast Wordpress Optimised Hosting.

Get Wordpress Hosting

WordPress is well-known as a blogging platform. Using some of the capabilities in-built in WordPress or using some plugins, it can be extended into a good Content Management System (CMS). The major difference between a blogging tool and a CMS is the ability to organize the content.

Quite often, when bloggers use WordPress for long, there is a need for better organization of their content. When followers increase, the ability to find posts by more than one criterion is required. The in-built categories and tags do not suffice. It is at this juncture that custom taxonomies come handy.

##Use case

For the purpose of this tutorial, let us assume that you possess a blog where you record your thoughts on your various interests -- politics, music, movies and books. You use categories to differentiate posts belonging to these various categories. As you are a movie buff, you review movies very frequently and your movie review posts are comparatively more. You want to organize them separately by genre or by language. You want your followers to be able to browse your movie reviews by genre or language, each indexed on a different page.

To meet this need (or a similar need), you have to create custom taxonomies and then use them to create index pages.

##Creating Custom Taxonomies

The first question that arises is why can't the in-built categories be used?

Well, categories can be used, if your site had only movie reviews and you wanted to organize them by only one criterion, i.e., genre. In this case, categories can replace genres and you can go ahead and use categories. If you do not want genres to be hierarchical, then you can use tags instead.

But, when we want to organize content by more than one criterion, then custom taxonomy is the way forward.

You can create Custom Taxonomies by two different methods: the non-coding method is to use a plugin and the other method is by adding a small piece of code. You can choose either one based on your needs.

Let us look at the non-coding method first and create a custom taxonomy using a plugin.

###Custom Taxonomy Using Plugins


There are several plugins available to create custom taxonomies. While you may try any of those, let us look at one of them, Types.

Login to your WordPress site as admin.

From the left sidebar, click on Plugins->Add New and search for Types. From the results, click on Install Now next to the Types plugin.

After successful installation of the plugin, click on Plugins->Installed Plugins and click on the Activate link below Types in the listing.

####Creating a custom taxonomy 'Genre'

Now, let us create a custom taxonomy Genre. From the left sidebar, click on Types->Custom Taxonomies.

In the screen above, click on the blue button Add New Taxonomy. In the Add New Custom Taxonomy screen that appears, fill in the details in the Name and description section. First, the plural name of the taxonomy - Genres, then the singular name - Genre, the slug (the url-version of the taxonomy name) - genre and a description that you feel fit.

The next section is Labels. Here, you may choose to customize the strings used or leave them as is. The default values suit the purpose. The %s in the strings will be replaced by the taxonomy name, in our case 'genre'. So, in the image below, Search %s means that the label will contain Search genres for the genre taxonomy.

The last section is Options. These options are advanced settings which you may not use most of the time. But, if you need to, here is a brief note on each.

On the Visibility section on the right side, let the default option be. If you choose Hidden, you will not see the taxonomy listed under Posts-> on the left sidebar.

In the Select Post Types section, check on Post. Here we assume that all the movie reviews will be posts. You can choose Page and Media if the particular taxonomy that you are building is applicable to these post types as well.

Click on the blue button Save Custom Taxonomy.

We are done for the moment. In future, if you want to make changes, you can click on Types->Edit Custom Taxonomy from the left sidebar.

You can test this by checking for presence of Posts->Genres link on the admin dashboard. Check the navigation menus for the presence of Genres.

Add genres to posts

When you open the post editor for an existing post, or add a new post, the post editor has a new metabox with the taxonomy name. In our case, a Genres metabox is displayed. You can click on the +Add New Genre link to add one or more genres.

Check on the genres from this metabox to add one or more genres to each review post.

If you have already published several posts for which you want to add genres, you can go to Posts->All Posts, choose multiple posts from the listing and do a bulk edit of the posts. Check against the posts, Choose Edit from the Dropdown box on top and click on Apply. You get a screen like below. Add genres in bulk and click on Update.

Quick editing of genres from the list of posts is also possible. Click on Posts->All Posts. Hover on any post in the list and click on the Quick Edit link that appears below. You can see a Genres box. Change or add genres quickly to posts using this feature.

After adding genres to a few review posts, you can also add a tag cloud to your sidebar. Click on Appearance->Widgets, drag a tag cloud widget to the sidebar. Choose Genres from the dropdown box and Save the widget. Now look at the sidebar in the site.

Managing the Genre taxonomy

If you have set show_admin_ui in the Options section of the settings, then it is possible to manage the taxonomy items for Genre by clicking on Posts->Genres.

This screen is very similar to the category/tag management screen.

There is a listing of popular genres. You can add a new genre item, by giving it a name and a slug. If it is a hierarchical taxonomy (as we have chosen for Genre), you can also choose a Parent genre. Finally, you can add a description.

You can also search for a genre as the number of genres increase.

There is also a listing of genres already added. You can edit or delete a particular genre from here. Just hover the mouse over the specific genre item to see the options below.

Clicking on View will show all posts that are under the specific genre.

##Custom Taxonomies Through Code

If you are slightly savvy with code, but do not know the specifics of Php or WordPress, you can attempt a lighter mechanism to add custom taxonomy. As you see, the Types plugin has so many more features (like Custom fields, User Fields and Post Types) which we don't need and when we use such feature-rich plugins of which we use just a fraction, it slows down the site.

An alternative is to create the custom taxonomies through code.

For the purpose of illustration, let us create another custom taxonomy for the movie reviews, Language.

Instead of writing the code for custom taxonomy, one can get the code generated using the taxonomy generator tool. Head to this link. There are two menus here. We shall be concerned only with the secondary menu.

Click on Taxonomy. The options here are very similar to the one in the options page of the Types plugin. Fill the taxonomy key (taxonomy name, 'language') and its singular and plural forms. Choose the post types, comma-separated if they are more than one. For language, we shall type only post. Choose whether it is hierarchical. As language need not be a hierarchical, choose 'no'.

Click on Labels. This is again similar to the labels option we had in the Types plugin. But, instead of the %s that was the default value that automatically substituted the taxonomy name in the plugin, here, we need to manually change the occurrences of Item to Language. For example, change New Item Name to New Language Name.

The next three tabs is Visibility, Query and Permalinks are similar to the options we described in the plugin. Refer to the options in the plugins section for the details.




The Capabilities tab refers to who can manage this taxonomy. This is to ensure that certain roles can only make changes. The defaults are fine.

The last tab is Other, which has the Update Count Callback, discussed in the plugin options.

After choosing your options, click on the Update Code green button to generate the code for creating the new taxonomy. Copy the code generated below.

Open your code editor by clicking on Appearance->Editor. From the list of files shown on the right, click on functions.php. Paste the code at the end of the file.

Click on Update File.

You are done. This achieves the same result without using a plugin.

Test it, by looking at Posts->Languages link, the navigation menus, the tag cloud widget and the admin column.

Add language to review posts using the metabox that appears in the post editor. The metabox is different from that of the Genre taxonomy because Language is non-hierarchical. It appears similar to the Tags metabox.

##Creating an Index Page

Adding a custom taxonomies Genre and Language to all review posts is done. But what is the use of such organization if it cannot be presented to the reader. So, now, let us create an index page where the reader can browse the movie review posts by genre.

The following section is of slightly technical nature. If you want to leave out the nitty-gritties, skip it and go directly to the Code section, that follows the next section.

###Creating the page template

To display the posts by a particular taxonomy, we need to create a page template first.

For this, firstly, we have to get all the terms of the particular taxonomy. We use get_terms function for the same.

    // get all the terms of the genre taxonomy
    $myitems = get_terms('genre', 'orderby=asc&hide_empty');

The above bit of code returns all the genres in the "Genre" taxonomy into $myitems.

Now we need to go through each genre and list out the posts that are of the specific genre. We use a foreach to loop over the genres. For each genre, we create a custom WordPress query and query the database.

    foreach ($myitems as $item) {
        $args = array('genre' => $item->name,'posts_per_page'=>-1);
        $my_posts = new WP_Query($args);

The posts_per_page is set to -1 so as to retrieve all posts that fit the criteria. Now if the query does return some posts, we need to list out the post titles.

    if($my_posts->have_posts()) : 
        echo $item->name; // Print the genre, e.g., Humour
        while($my_posts->have_posts()) : $my_posts->the_post(); 
            the_title(); // print the title of the post.

We're almost there. It is more reader-friendly to include links to the posts instead of displaying the titles. So, let us alter the the_title(); line a bit so that we include the permalink.

    if($my_posts->have_posts()) : 
        echo $item->name; // Print the genre, e.g., Humour
        while($my_posts->have_posts()) : $my_posts->the_post(); ?>
            <div class="entry-content"> <a href="">
             </a></div> // print the title of the post.
        <?php endwhile; 

Now, we need put all these things together in the right place.


The code for the twenty-fifteen theme is shown here. If you are using some other theme, it is for you to figure it out with a little bit of exploration and experimenting.

In your wp-content/themes/twenty-fifteen/ directory, make a copy of the page.php file and rename it to genres-page.php.

Open this file and you can see something similar to the image below. The portion selected in gray is the portion we shall replace with the above code.

Remove this code. Replace it with the following code.

    <article >
    <header class="entry-header">
    <?php the_title( '<h1 class="entry-title">', '</h1>' ); ?>
    </header><!-- .entry-header -->

<div class="entry-content"> $item->name,'posts_per_page'=>-1); $my_posts = new WP_Query($args); if($my_posts->have_posts()) : ?> <h3> name; // Print the genre, e.g., Humour ?> </h3> have_posts()) : $my_posts->the_post(); ?> <div class="entry-content"> <a href=""> </a></div>

</div> </article>

Save the file.

Now, we have created a template for listing out the review posts by genre. You could create a similar one for language as well by using the code below.

    <article >
    <header class="entry-header">
    <?php the_title( '<h1 class="entry-title">', '</h1>' ); ?>
    </header><!-- .entry-header -->

<div class="entry-content"> $item->name,'posts_per_page'=>-1); $my_posts = new WP_Query($args); if($my_posts->have_posts()) : ?> <h3> name; // Print the language, e.g., French ?> </h3> have_posts()) : $my_posts->the_post(); ?> <div class="entry-content"> <a href=""> </a></div>

</div> </article>

###Creating the Browse by Genres Page.

The last bit of glue to add is to create the actual page from the template.

Before doing that, reopen the genres-page.php file. At the top, there are a few lines of comment giving details about the file.

Add the following line here.

    * Template Name: Genres-template

Save the file and close it.

Now, on the WordPress admin dashboard, click on Pages->Add New from the left side bar to create a new page. Type the title as Browse by Genres. Now, on the right side, in the Page Attributes metabox, you will find an extra field called Template with a dropdown box. The Genres-template will be listed in this dropdown box, choose this and then Publish the page.

View the Page and it will look similar to the one below.

It does look a little plain, but it has the functionality for an index page where one can browse by genres. You can add some colour by working on your CSS, if you so wish.

Create a page for the languages taxonomy with a languages template, following the same method.

###Add the index pages to menu

Optionally, you can also add these pages to your menu. Click on Appearance->Menus. Check on the Browse by Genres and Browse by Languages pages. Now, click on Add to Menu button. You will see the pages being added to the Menu Structure on the right. Now click on Save Menu.

Visit the site and you will see the two pages on your Menu. Now, your readers have an easy way of browsing your movie reviews either by genre or by language.

That was an example of what can be done with custom taxonomies in WordPress to make organization of your site content easy.

Using Wordpress? Get our lightening fast Wordpress Optimised Hosting.

Get Wordpress Hosting