If you have lots of posts in your WordPress site, you have probably used the Category and/or Tag filters at the top of the post list page. These filters are great because they very quickly allow you to limit the kind of posts that are displayed, and let you find the one(s) you are looking for with relative ease. Well, if you use any custom post types or custom taxonomies, then you have probably noticed that these options are not available. So I’m going to show you how to add new filters for your custom taxonomies to any custom post type you have registered on your site.
The process is pretty simple, in that it does not require a lot of code, and WordPress does most of the work for you, but the function itself is a little bit complex. What we are going to do is setup a function that creates a drop down select menu based off of the terms in the taxonomies we provide. So if we set the function to display a filter for the “genres” taxonomy, it will retrieve a list of all of the terms in that taxonomy (all terms that are not empty), and then display each of those terms as an option in the drop down menu. If we provide multiple taxonomies, through an array, then the function will create a drop down for each set of taxonomy terms.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | <?php function pippin_add_taxonomy_filters() { global $typenow; // an array of all the taxonomyies you want to display. Use the taxonomy name or slug $taxonomies = array('faq_topics'); // must set this to the post type you want the filter(s) displayed on if( $typenow == 'faqs' ){ 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', 'pippin_add_taxonomy_filters' ); |
The function is then passed through the “restrict_manage_posts” action hook in order for the select menus to be displayed in the post list page.
If everything worked correctly, you should see something like this:

Note that in the function there is a check for the current post type. If you do not set the conditional $typenow statement to the post type you want the filter displayed on, it will show on ALL post type pages, and since not all of your post types may have the custom taxonomy, this is not generally good idea.
If you are a user of my Easy Content Types plugin, then you will see these taxonomy filters coming very soon with an add-on plugin.

Very nice. Thanks for the great tip. I had some code that struggled to do this, but never seemed to work out…
This was one piece of it:
// ***Add Filter for CPTs by Custom Taxonomy***
// Filter the request to just give posts for the given taxonomy, if applicable.
// add ‘taxonomies’ => ‘category’ // Add tags and categories taxonomies – To CPT register args to limit specific filter
function taxonomy_filter_restrict_manage_posts() {
global $typenow;
if ( is_singular( ‘we_books’ ) )
return;
// If you only want this to work for your specific post type,
// check for that $type here and then return.
// This function, if unmodified, will add the dropdown for each
// post type / taxonomy combination.
$post_types = get_post_types( array( ‘_builtin’ => false ) );
if ( in_array( $typenow, $post_types ) ) {
$filters = get_object_taxonomies( $typenow );
foreach ( $filters as $tax_slug ) {
$tax_obj = get_taxonomy( $tax_slug );
wp_dropdown_categories( array(
‘show_option_all’ => __(‘Show All ‘.$tax_obj->label ),
‘taxonomy’ => $tax_slug,
‘name’ => $tax_obj->name,
‘orderby’ => ‘name’,
‘selected’ => $_GET[$tax_slug],
‘hierarchical’ => $tax_obj->hierarchical,
‘show_count’ => false,
‘hide_empty’ => true
) );
}
}
}
add_action( ‘restrict_manage_posts’, ‘taxonomy_filter_restrict_manage_posts’ );
There was additional code, much more than you have there… I never got a grip on understanding it.
Yours works great!
Yeah, I can see a couple of problems with that code. Glad mine works for you
Once you have selected a taxonomy filter you cannot select another without first going back to the full custom post type listing – otherwise gives “invalid post type”.
Does the paid-for plug-in fix this issue?
By paid plugin, do you mean Easy Content Types and the ECPT Filter by Taxonomy add-on?
Hi Pippin. Thanks for posting this code, I just added it to my Portfolio Post Type plugin: https://github.com/devinsays/portfolio-post-type/commit/6664a1dff46257187689ed3aa8ca6f7447c04458
I did notice one issue. $_GET[$tax_slug] was throwing an isset notice when you weren’t in the taxonomy view. I fixed this by checking for it explicitly:
$current_tax_slug = isset( $_GET[$tax_slug] ) ? $_GET[$tax_slug] : false;
Oh yeah, the isset() should definitely be there. Thanks for noticing.
Beautiful! You make me look like a rockstar! Thanks for that awesome snippet!