How to Add a Taxonomy Filter to your admin list for Custom Post Type

This tutorial explains you to enable “taxonomy filter” for any of your custom post type you created, in listing view of custom post type. This will allow you to filter the entries of custom post type by the term within a custom taxonomy attached to that post type.

For example, if you have a custom post type called “Books”, with a taxonomy called “Genre” attached to it, you could enable the filter for Genres and then be able to view only books filed in the “Fantasy” genre from the admin mange posts screen.

Add this below function to your theme functions.php




function book_add_taxonomy_filters() {
global $typenow;

// an array of all the taxonomies you want to display. Use the slug
$taxonomies = array(‘genre’);

// must set this to the post type you want the filter(s) displayed on
if( $typenow == ‘book’ ){

foreach ($taxonomies as $tax_slug) {
$tax_obj = get_taxonomy($tax_slug);
$tax_name = $tax_obj->labels->name;
$terms = get_terms($tax_slug);
if(count($terms) > 0) {
echo "<select name=’$tax_slug’ id=’$tax_slug’ class=’postform’>";
echo "<option value=”>Show All $tax_name</option>";
foreach ($terms as $term) {
echo ‘<option value=’. $term->slug, $_GET[$tax_slug] == $term->slug ? ‘ selected="selected"’ : ”,’>’ . $term->name .’ (‘ . $term->count .’)</option>’;
echo "</select>";
add_action( ‘restrict_manage_posts’, ‘book_add_taxonomy_filters’ );