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 😉
this works really well, thanks for the tut!
Thanks Craig.. i’m happy with this one. I’m definitely going to continue using this on client sites when it fits.
Hello,
where do i have to insert the code?
This works great, thanks! – Brian
Thanks, loving this one still!
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).
Really it worked like charm !
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!
Great! Thanks for stopping by 😉
great add, thumb up!
thank you both for this snippet, after 5 years still works like a charm.
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.
Whoa!!!! Does that really work? Will have to try. Super lean! Thanks.
yeah! works great this way, thanks to both of you! 🙂
With function (tsm_convert_id_to_term_in_query) didnt work – shows no post on selected category.
With ‘value_field’ => ‘slug’,
works well
Thanks for confirmation. Looks like Matias has the more efficient version. I’ll have to update the snippet after I test.
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
Great, Thanks 😀
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?
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?
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.
How can i add this with multiple custom taxonomies?
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.
You will need to try with different approach. Something like this:
https://pippinsplugins.com/post-list-filters-for-custom-taxonomies-in-manage-posts/
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!
And you’re sure the post_type and taxonomy names are correct?
Simple, easy, it works very well. Thanks!
Great!
Sweet! Thanks for this post! Worked like a charm…
Woot! Glad it helped 😉
Excellent! Thanks a lot.
It’d be great to have something like this for media.
JJJ recently released something awesome for this. WP Media Categories
Awesome! But doesn’t work with current media uploader… Any ideas?
What does a taxonomy filter have to do with the media uploader? Or, am I missing something here?
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.
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.
Thanks …. 🙂
Cheers 😉
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.
ok never mind found it, its at the last of functions.php
possible to create a plugin out of it?
You certainly could. I often put this type of code in custom site functionality plugins. You could do it quickly with something like Pluginception.
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”
That sounds like you have something else going on. Did you try with other plugins deactivated? Maybe with a different theme too?
I’m having the same problem. Filter only works once this does the same thing.
This is gold!
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.
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!
It was my fault. Problem was $query->set( ‘post_type’ …… added into another part of code.
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.
Very interesting point, thanks for this!
I know it’s been a year, but this saved me. Thank you!!
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?
There is always a way! But sadly, not one that I have tried to figure out yet 😉 Sorry!
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.This works !! Thanks for the great tut, JiveDig.
Great job. Very useful!
Thnx!
Much appreciated, Mike!
Woot!
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.
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!
this post help me more it’s very useful.
Thanks you.
is_numeric($q_vars[$taxonomy]) returns true for “0” (string). Better to check as (int) $q_vars[$taxonomy].
Great work and excellent post! This is really helpful to used my business site development.
Thanks.
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
I’ve found a way to do what i needed,
just replace the taxonomy by the custom field slug
and it works 🙂
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 !
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?
I use FacetWP all the time, but it doesn’t create taxonomies. FacetWP only uses taxonomies that already exist.
Thanks for the provided information. Saved a lot of time..Cheers….
Thanks, this is very useful indeed! 🙂
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
What if I want to filter custom post via post Meta?
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;
}
}
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
Very useful. Will be featuring this snippet in a WordPress help series.
How can i remove the count from parent ?
It would think changing ‘show_count’ to false would be the first thing to try.
This is greate
thanks
Great post, very useful indeed!
Tanks a lot
Brilliant! This was extremely helpful to me. Thanks for sharing!
Thank you very much! Very useful!!! 🙂
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 )`
Great call. Snippet updated. Thanks!
Works perfectly! Thanks a ton for this post Mike!
Work like magic, thank you
Nice work
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.
Hello
Instead of displaying drop-down in back-end is there any way to show in frontend
Woow Its work for me, Thanks!
This saved me lots of hassle, thank you so much for this code! Works perfectly.
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
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
Where in wordpress do I insert this snippet? thanks
The typical places are in functions.php of your theme, a custom functionality plugin, or a Code Snippets plugin.
I’m sorry, where the snippet?
GitHub must’ve glitched. It’s there now.
Worked like wow!!
Beautiful script!
How could it be modified to show “only” the parent categories?
Thanks.
Thank you JiveDig, a real “plug and play” code!
WOW!!! great snippet!! thank you so mucho. It works great.
Perfect! Thanks for sharing this. Saved me a lot of time.
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!