7 days of WordPress plugins, themes & templates - for free!* Unlimited asset downloads! Start 7-Day Free Trial
  1. Code
  2. Plugins

WordPress Custom Post Types: Taxonomies, Admin Columns & Filters

Scroll to top
Read Time: 10 mins
This post is part of a series called A Guide to WordPress Custom Post Types.
A Guide to WordPress Custom Post Types: Creation, Display and Meta Boxes

Before diving into this tutorial please go through my first tutorial on Custom Post Types, where I have explained some of the important aspects of CPTs (Custom Post Types). In this tutorial we shall explore more about this wonderful feature in WordPress.

This tutorial covers how to create custom taxonomies, admin columns, taxonomy filters and an archive page for your CPT. So let's begin.

Custom Taxonomy for Custom Post Types

Taxonomies are a great way to group things together and help us to search posts belonging to a specific group. In WordPress we generally use Categories and Tags as taxonomies. The steps given below explain how to create custom taxonomies for your CPT.

Step 1 : Register Custom Function

Open your plugin file, in our case Movie-Reviews.php and add the following code to register the custom function.

Step 2 : Implementation of Custom Function and Registering Custom Taxonomy

Here the register_taxonomy function does all the hard work of creating a custom taxonomy (in our case a category) with the name 'movie_reviews_movie_genre' for the custom post type 'movie_reviews'. The labels define the different strings used in the admin section of the taxonomy.

  • 'show_ui' => true is used to make the taxonomy editor visible in the dashboard.
  • 'show_tagcloud' => false decode whether the tag cloud should be visible. In our case it's disabled.
  • 'hierarchical' => true decodes the format of the custom taxonomy.

Note: 'hierarchical' => false converts the categories into tags.

Step 3 : Displaying Custom Taxonomies

After saving the Movie-Reviews.php file, open your custom template file, in our case single-movie_reviews.php and add the following highlighted code to make the categories visible in our posts.

Step 4 : The Result

Here we have added a custom taxonomy 'Movie Genre' in our movie reviews CPT. Now we will be able to add new categories from the admin panel and assign each of them to our CPT.

Displaying Additional Columns

In the WordPress admin CPT listing page, by default two columns exist - Date and Comments - through which we can sort those CPT items. In order to add extra columns and sorting please follow the steps given below.

Step 1 : Register Function

Open the plugin file Movie-Reviews.php and add the following line of code to register a function to be called when the Movie Reviews listing page is being prepared.

Here we have used the variable filter manage_edit-(Custom_Post_Type)_columns, which passes the column list of the CPT as an argument to the function.

Step 2 : Implementation of the Function

Here we have added two columns: Director and Rating in the admin panel of the CPT and also deleted the Comments column from the listing.

Step 3 : Populating the Columns

Register a function to populate the columns.

Step 4 : Implementation

Here since the function gets executed when any of the CPT columns is rendered, it checks for the currently requested columns before echoing them. We have used the get_the_ID() function to retrieve the index of the current row and then in turn have used the get_post_meta to retrieve the data in the column.

Step 5 : Register Columns as Sortable

Now let us register a function to be called when WordPress identifies sortable columns in CPT.

Step 6 : Implementation

This function identifies two columns to make them sortable and then returns the array. But our work hasn't finished yet.

Step 7 : Order by Custom Field

The above function is associated with the request filter and adds elements to the query array, based on the variables in the query URL. Actually WordPress doesn't know how to order by the fields 'Movie Director' or 'Movie Rating', so we need to teach WordPress how to do that through this function.

We have successfully added two sortable columns in the admin section.

Creating Filters With Custom Taxonomy

Here we shall see how custom taxonomies (in this case, categories) can be used as an additional filter in the CPT listing page in the WordPress admin, so that administrators can display CPT elements belonging to a specific category.

Step 1 : Register the Function

Open your plugin file and add the following code to register a function to be called when WordPress is preparing to display the filter drop down list.

Step 2 : Implementation of the Function

Here we have used a global variable to know the type of post that is being displayed and also used a post query variable to check if there is already an existing filter and accordingly set the filter. The wp_dropdown_categories function is used to display all the taxonomies registered with Movie Genres. The 'orderby', 'show_count', 'hide_empty', 'depth' etc. are different arguments specifying sorting, show items count on each category, hide non associated categories, determine the maximum depth to be shown for the hierarchical categories respectively.

Step 3 : Display Filtered Results

Now after the filter drop down list has been prepared, we shall write some code to display the filtered results. Register a function to be called when the post display query is prepared.

Step 4 : Implementation of the Display Function

The perform_filtering function receives the current WordPress post query object and then commences by getting a pointer to the query variables stored inside the query object. Then it verifies if a Movie Genre is a part of the query variables and then executes the query.

Now you will be able to use the filter to display the Movies by their Genres.

Last but Not the Least: Create an Archive Page

As we have created a custom template for our CPT, we can also create a custom archive page overriding the default archive template.

Step 1 : Adding a Fallback to the Archive Template

Open the plugin file Movie-Reviews.php and the add the highlighted code in the include_template_function function.

WordPress searches through the theme directory for an archive template file before using the default one. This function checks if the user has provided an archive template in the theme directory else it looks for the file in the plugin's folder.

Step 2 : Create the Archive Template

Save and Close the plugin file and then create a new file called archive-movie_reviews.php and add the following code into it.

Here we have used the loop to cycle through the post entries and then display them using a table layout. We have also defined a navigation menu if there are more items than the maximum number configured under WordPress Settings. Navigation menus are displayed using the next_post_links and previous_post_links functions.

We have used the global wp_query object containing the data about the currently executed query, in order to render the page contents. The get_post_meta function has been used to retrieve custom field data.

Step 3 : The Result

Save the file and check the archive page for the Movie Reviews archive list.

Here we come to the end of this tutorial. I hope you have been able to grasp the significance of Custom Post Types. There is more to explore, just play around with it.

Thanks for reading and feel free to give your feedback.

Did you find this post useful?
Want a weekly email summary?
Subscribe below and we’ll send you a weekly email summary of all new Code tutorials. Never miss out on learning about the next big thing.
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.