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.

  1. Bruce

    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!

    • Pippin

      Yeah, I can see a couple of problems with that code. Glad mine works for you 🙂

  2. RegisIT

    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?

    • Pippin

      By paid plugin, do you mean Easy Content Types and the ECPT Filter by Taxonomy add-on?

    • Pippin

      Oh yeah, the isset() should definitely be there. Thanks for noticing.

  3. Alicia St Rose

    Beautiful! You make me look like a rockstar! Thanks for that awesome snippet!

  4. Zac

    This is great, thank you, so much easier than some of the stack overflow tuts.

    One question, where does that addition ‘isset’ line go that Devin pointed out, just under the ‘$terms = get_terms($tax_slug);’ line?

    Thanks again,

  5. Kareem

    Great It’s work well with me and enough Thank You 😉

  6. Ashwin Kumar

    Thanks bro.. Its working awesome. 🙂 Saved my lots of time and headache.

  7. Giovanni

    Can you help me to made this part of code:
    Show All $tax_name
    ready with any dashboard language?

  8. Kevin

    Thanks a lot. Works great.

  9. Chanchal RK

    Hi! I want to add this functionality, but on my front end!
    I have searched so much, but haven’t found a comprehensive solution!
    Can anybody help?

  10. Gen

    Thank you. I’ve been trying to find a simple solution for this for weeks.

  11. Didar

    Thank you!

  12. Josh.Horneck

    I’d really like to add this code into my custom plugin, next to the taxonomy I registered for it. What needs to be done to make it work there? (It works in functions, but not in the plugin file.)

    • Pippin

      The code should be identical regardless of where it is. In what way does it not work from the plugin?

    • Josh.Horneck

      Yea, I’m not sure what was going on, because I thought it would act the same. Whatever I was doing last week I’m not doing now, because it works. It just didn’t show the dropdown. Thinking I may have forgotten to actually add the action perhaps. Great stuff, as always! Thanks for the reply!

  13. Dima

    nice job!!!!

  14. Teresa

    Thank you, this works perfectly!

  15. Romy

    Great snippet. Thank you. I’m using this with Woocommerce which has a filter for its categories. When I add filters for my taxonomies, they appear before WC categories, I prefer them to go after. Is there a way to specify the order of the filter(s)?

    • Romy

      Never mind. I worked it out :).

  16. David

    Very nice function! I’ve saved for all my custom post types snippets…

  17. Eldor

    This post is just what I really needed! It works!

    Many thanks for sharing!

  18. dariodev

    Thank you for this simple and efficient solution!

  19. John Fotios

    I’ve been wanted this since using the Toolset Types but could never find the option. Good to know there’s a simple way to do it through functions (by simple, I mean I can copy paste your code!).

    Thanks

  20. igestalten

    Nice snippet! Thanks!
    I got a problem when using slugs with capital letters. The Filter was not applied. Forcing the name attribut of the to lowercase – strtolower($tax_slug) – fixed that for me.

    • Pippin

      Good catch!

  21. flatpackmusic

    this works beautifully, thank you – i’d like to run it twice though, showing different taxonomy filters on different post types. is this possible? after declaring, if i repeat it from the ‘global $typenow;’ instruction i don’t see the second of the two filters coming up. have you done this before? how did you manage if so?

  22. Yaroslav

    Thanks!
    It`s very useful solution.

  23. emma

    Its work!! thanks

  24. damasovelazquez

    Fantastic content!!

    Thanks.

  25. Ben

    Still a great solution in 2020!

Comments are closed.