This is a powerful little snippet to allow filtering your post or custom post type by a specific taxonomy term in the WordPress admin.
In my use case I had a custom post type for ‘Team’ members. I then had a custom taxonomy called ‘Group’ that associated each member with one or more groups within the organization. The client wanted to be able to filter all Team members by Group in the admin.
My first thought was to use sortable columns, but that would still require scrolling and possibly paging to get the right people.
I tried a few things and ended up with this nice snippet…
The only thing you need to change is your post type and your taxonomy name. The rest is pretty much magic 😉
Craig Grella says
this works really well, thanks for the tut!
JiveDig says
Thanks Craig.. i’m happy with this one. I’m definitely going to continue using this on client sites when it fits.
Terry says
Hello,
where do i have to insert the code?
Brian Shim says
This works great, thanks! – Brian
JiveDig says
Thanks, loving this one still!
Neil Simpson says
Worked like a charm, I’ll use this a lot. I’d love to get the show_count and hide_empty working for an author filter too but it only seems to work for taxonomies (unless I’m screwing something up).
Pronay says
Really it worked like charm !
Agustin says
Fantastic, very usefull, I only added one parameter to wp_dropdown_categories function for hierarchical taxonomies:
wp_dropdown_categories(array(
...
'hierarchical' => 1,
));
Thankyou very much!
JiveDig says
Great! Thanks for stopping by 😉
Gabriel says
great add, thumb up!
thank you both for this snippet, after 5 years still works like a charm.
Matias says
Usefull code!
Just add
wp_dropdown_categories(array(
...
'value_field' => 'slug',
));
and you don’t need ‘tsm_convert_id_to_term_in_query’ function anymore.
JiveDig says
Whoa!!!! Does that really work? Will have to try. Super lean! Thanks.
Piotr says
yeah! works great this way, thanks to both of you! 🙂
DEr says
With function (tsm_convert_id_to_term_in_query) didnt work – shows no post on selected category.
With ‘value_field’ => ‘slug’,
works well
JiveDig says
Thanks for confirmation. Looks like Matias has the more efficient version. I’ll have to update the snippet after I test.
Pradhumn says
still now working for me. doesn’t show any item after selecting. can you send the full code to how to use ‘value_field’ => ‘slug’. Thank You
Deepak Ji says
Great, Thanks 😀
JS says
Just tried it but it doesn’t work 🙁
I see the dropdown menu but when I’d like to show only posts of type1 I still see all posts.
Any idea?
JiveDig says
Strange, i’ve not had that issue yet. I don’t use this all the time though, so i haven’t put it through any crazy testing.
Naturally, you’ve got the correct post type and taxonomy (not term) name in both places?
Rob says
I have the same problem.
I get the dropdown.
But when I select the taxonomy I want to filter by, and hit ‘Filter’, I get all posts back. No filtering has happened.
Rob says
How can i add this with multiple custom taxonomies?
JiveDig says
I’m not sure if this method will work to add multiple taxonomies to the same dropdown. Using this method, you’d probably have to duplicate the functionality to add a second taxonomy as a new dropdown.
Luko Del Ponte says
You will need to try with different approach. Something like this:
https://pippinsplugins.com/post-list-filters-for-custom-taxonomies-in-manage-posts/
Mike says
Same for me! The Filter box appears, but the dropdown is blank. Tried with original code, and with value_field=slug version, same results… doh!
JiveDig says
And you’re sure the post_type and taxonomy names are correct?
Francesco says
Simple, easy, it works very well. Thanks!
JiveDig says
Great!
Joel Bratsch says
Sweet! Thanks for this post! Worked like a charm…
JiveDig says
Woot! Glad it helped 😉
Marek says
Excellent! Thanks a lot.
It’d be great to have something like this for media.
JiveDig says
JJJ recently released something awesome for this. WP Media Categories
acy says
Awesome! But doesn’t work with current media uploader… Any ideas?
JiveDig says
What does a taxonomy filter have to do with the media uploader? Or, am I missing something here?
Darto KLoning says
Your code is great and work. Even though if the taxonomy support for array would be much more great. BUT, all is good, copy paste not hurt at all :D. Thanks.
JiveDig says
I agree. According to the wp_dropdown_categories Codex, ‘taxonomy’ only takes a string, not an array. There are certainly other ways to do it, but this method worked for me last I needed it.
freelance web designer kerala says
Thanks …. 🙂
JiveDig says
Cheers 😉
amrish pandey says
hi admin i have replace my custome post time slug and taxnomy slug in the above code. i am not sure where to add this snippet . please guide.
amrish pandey says
ok never mind found it, its at the last of functions.php
tobias says
possible to create a plugin out of it?
JiveDig says
You certainly could. I often put this type of code in custom site functionality plugins. You could do it quickly with something like Pluginception.
Tim says
Hi – this code only works for me the first time. If I filter a second time, I get all kinds of junk added into the URL:
post_type=Array&_wpnonce=eaf3a1bb38&_wp_http_referer=….
The result is great – the first time. Any subsequent filters return “invalid post type”
JiveDig says
That sounds like you have something else going on. Did you try with other plugins deactivated? Maybe with a different theme too?
Kirby Kalbaugh says
I’m having the same problem. Filter only works once this does the same thing.
Robert says
This is gold!
Mark says
Not work properly if have few different custom post types uses the same taxonomy. Then display all posts with this taxonomy, doesn’t matter post type is.
JiveDig says
I would guess that instead of using $typenow to check the post_type, you should manually enter your post_type name, so it only checks the one you are going for? Interesting bug you found. I’d have to play with it to solve, let us know if you find a solution!
Mark says
It was my fault. Problem was $query->set( ‘post_type’ …… added into another part of code.
Phillip says
I also struggled to get this to work, and wanted to share the solution I found at http://wordpress.stackexchange.com/questions/578/adding-a-taxonomy-filter-to-admin-list-for-a-custom-post-type. In my case, the filter box appeared correctly, but the filter didn’t actually work.
First, when registering the post type, the ‘query_var’ parameter must be set to true:
register_post_type( 'my_post_type', array (
...
query_var' => true ) );
Next, as mentioned in earlier comments the ‘restrict_manage_posts’ function should contain:
wp_dropdown_categories(array(
...
'value_field' => 'slug',
));
And last, the ‘parse_query’ filter wasn’t needed anymore.
JiveDig says
Very interesting point, thanks for this!
Danielle says
I know it’s been a year, but this saved me. Thank you!!
Lisa says
Works like a charm! Is there any way to add an option to filter out posts that don’t have anything filled in yet for that taxonomy?
JiveDig says
There is always a way! But sadly, not one that I have tried to figure out yet 😉 Sorry!
Daron says
If you want to show a hierarchical dropdown (with indentation for child terms) you only need to add one line to the
wp_dropdown_categories()
arguments.'hierarchical' => 1,
will add the indentation similar to the categories widget.Sneha says
This works !! Thanks for the great tut, JiveDig.
loveitgroup says
Great job. Very useful!
Thnx!
Gabriel says
Much appreciated, Mike!
JiveDig says
Woot!
Robert Huska says
This is an awesome approach, thank you! I modified it a bit to be able to use it on multiple CPTs and their associated custom taxonomies. In each of your functions I added an array and wrapped your if statements in a foreach loop
$cpt_array = array(
‘cpt_case-study’ => ‘ct_category’,
‘cpt_team-member’ => ‘ct_department’
);
foreach ($cpt_array as $post_type => $taxonomy) {
// Your if statements here
}
I would love to see this setup packaged into a nice plugin that can me modified form the admin… But still, awesome as is.
Karthik says
if the logged in user wants to see own posts listing under ‘Mine’ link. How can we do that.
I tried
unset($q_vars[‘author’]);
$q_vars[‘author__in’] = $tax_terms;
which is working fine, but the POST COUNT shows differently! Any help will be highly appreciable!
3nokor says
this post help me more it’s very useful.
Thanks you.
Mark Howells-Mead says
is_numeric($q_vars[$taxonomy]) returns true for “0” (string). Better to check as (int) $q_vars[$taxonomy].
wordpress taxonomy says
Great work and excellent post! This is really helpful to used my business site development.
Thanks.
jean-charles says
Hi Mike !
This works great !! thank you very much !
I wanted to know if you had any idea to make the same thing to filter a / many custom post type from acf ?
Thank you so much for what you’ve done and your possible help 🙂
Jean-Charles
jean-charles says
I’ve found a way to do what i needed,
just replace the taxonomy by the custom field slug
and it works 🙂
Sébastien says
Hi there,
Great Tutorial, very clear & helped me a lot !
I ‘ve worked on this approach and found useful to add a sorting functionality to the taxonomy column … Here is a little plugin that does the job :
http://www.concepteur-developpeur-web.fr/plugin-wordpress-filtres-colonnes-taxonomies-personnalisees/
(downloadable on WordPress Repo 😉
Thank’s again !
Joana says
Hi! Thank you for this code, this was very helpful. It worked, but no results came which I think was because the taxonomy was created by FacetWP, have you ever tried using it with it?
JiveDig says
I use FacetWP all the time, but it doesn’t create taxonomies. FacetWP only uses taxonomies that already exist.
Rajesh says
Thanks for the provided information. Saved a lot of time..Cheers….
Mark Law says
Thanks, this is very useful indeed! 🙂
NikkaG says
Simple, great, THANKS!!!
Also, thanks to Marusho, he improve you snipped and now multiple filters to a custom post type has been added https://gist.github.com/marushu/426626851afaadb1e3972de49c34b0b9
Darshan Saroya says
What if I want to filter custom post via post Meta?
Brendan Lawton says
You don’t need to add the second filter to modify the query, WordPress will take care of this for you if you add the ‘value_field’ param and set it to ‘slug’ in your wp_dropdown_categories($args) function in the first filter. As in:
__(“Show All {$info_taxonomy->label}”),
‘taxonomy’ => $taxonomy,
‘value_field’ => ‘slug’, // ADD THIS HERE
‘name’ => $taxonomy,
‘orderby’ => ‘name’,
‘selected’ => $selected,
‘show_count’ => true,
‘hide_empty’ => true,
));
};
}
// NONE OF THE BELOW NOW NECESSARY
/**
* Filter posts by taxonomy in admin
* @author Mike Hemberger
* @link http://thestizmedia.com/custom-post-type-filter-admin-custom-taxonomy/
*/
add_filter(‘parse_query’, ‘tsm_convert_id_to_term_in_query’);
function tsm_convert_id_to_term_in_query($query) {
global $pagenow;
$post_type = ‘team’; // change to your post type
$taxonomy = ‘group’; // change to your taxonomy
$q_vars = &$query->query_vars;
if ( $pagenow == ‘edit.php’ && isset($q_vars[‘post_type’]) && $q_vars[‘post_type’] == $post_type && isset($q_vars[$taxonomy]) && is_numeric($q_vars[$taxonomy]) && $q_vars[$taxonomy] != 0 ) {
$term = get_term_by(‘id’, $q_vars[$taxonomy], $taxonomy);
$q_vars[$taxonomy] = $term->slug;
}
}
Josh F says
This looks great! Been trying to look for solutions for a while now.
I am quite new to WordPress and just wondering where I can paste this php code?
Your directions would be greatly appreciated
KIJO says
Very useful. Will be featuring this snippet in a WordPress help series.
Luke says
How can i remove the count from parent ?
JiveDig says
It would think changing ‘show_count’ to false would be the first thing to try.
narayan says
This is greate
cris says
thanks
Agence Debord says
Great post, very useful indeed!
Tanks a lot
Rick says
Brilliant! This was extremely helpful to me. Thanks for sharing!
globdug says
Thank you very much! Very useful!!! 🙂
janw.oostendorp says
Great function. small thing
`__( “Show All {$info_taxonomy->label}” )`
is not correct for translation texts, poedit and others can’t handle this the correct format is:
`sprintf( __( ‘Show all %1$s’ ), $info_taxonomy->label )`
JiveDig says
Great call. Snippet updated. Thanks!
Karthik says
Works perfectly! Thanks a ton for this post Mike!
umehass says
Work like magic, thank you
FileMoro says
Nice work
Tejus says
This is great! However, it seems to work only for parent taxonomies. It shows the child taxonomies in the select drop-down correctly, but selecting a child throws a WP error.
Anjana says
Hello
Instead of displaying drop-down in back-end is there any way to show in frontend
Amelia Jones says
Woow Its work for me, Thanks!
Duncan Michael MacGregor says
This saved me lots of hassle, thank you so much for this code! Works perfectly.
Luigi says
Hi Mike,
thanks for this really helpful snippet.
Is there anyway to have a NOT function in it, as showing all the posts that don’t have the taxonomy in?
Thanks,
Luigi
Sumit Thakur says
How can I remove defult categories like product cat from product list at side of admin panel. Please refer below image and Please send solution on my email ID
https://prnt.sc/wnza8h
Emal ID :: incubers.sumit@gmail.com
gianmarco says
Where in wordpress do I insert this snippet? thanks
JiveDig says
The typical places are in functions.php of your theme, a custom functionality plugin, or a Code Snippets plugin.
joseph says
I’m sorry, where the snippet?
JiveDig says
GitHub must’ve glitched. It’s there now.
Mohit says
Worked like wow!!
Edgardo says
Beautiful script!
How could it be modified to show “only” the parent categories?
Thanks.
Flavio says
Thank you JiveDig, a real “plug and play” code!
Allan says
WOW!!! great snippet!! thank you so mucho. It works great.
Mack says
Perfect! Thanks for sharing this. Saved me a lot of time.
Stefan says
Hi, great snippet, thanks!
I was wondering if it is possible to remove the “filter-by-date” dropdown.
I sort by a custom acf-datefield and don’t want users to get confused.
thanks!