The Stiz Media, LLC

Making your website work for you

  • Styles for Genesis
    • FAQs
  • Cart
  • My Account
  • Home
  • Web Design
  • Portfolio
  • Resources
  • Blog
  • Contact

Powerful websites, with style.

Home » Blog » Custom Post Type Filter Admin By Custom Taxonomy

Custom Post Type Filter Admin By Custom Taxonomy

January 29, 2015 Andhi 103 Comments

CPT filter by taxonomy

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 😉

Related Posts

  • Use WP_Query To Alter The Posts Per Page Of A Custom Taxonomy Term Archive Of A Custom Post Type
  • Using Custom Post Types And Meta Fields To “Client-Proof” Adding New Content
    Using Custom Post Types And Meta Fields To "Client-Proof" Adding New Content
  • ACF local avatars
    Use Advanced Custom Fields Pro for Simple, Local Avatars

Tutorials Custom Post Types, Custom Taxonomies

‹ Different Templates For Parent And Child Category Archives
Show Field Keys in Advanced Custom Fields Pro ›

Try The Most Powerful Genesis Child Theme Ever Created

Mai Theme is the most customizable Genesis child theme (and plugin!) available. Check it out now!

Learn More @ MaiTheme.com

Our posts, directly to your inbox

Comments

  1. Craig Grella says

    January 30, 2015 at 2:57 pm

    this works really well, thanks for the tut!

    Reply
    • JiveDig says

      January 30, 2015 at 3:12 pm

      Thanks Craig.. i’m happy with this one. I’m definitely going to continue using this on client sites when it fits.

      Reply
    • Terry says

      November 23, 2021 at 5:23 pm

      Hello,

      where do i have to insert the code?

      Reply
  2. Brian Shim says

    March 24, 2015 at 2:09 pm

    This works great, thanks! – Brian

    Reply
    • JiveDig says

      March 24, 2015 at 2:10 pm

      Thanks, loving this one still!

      Reply
  3. Neil Simpson says

    April 8, 2015 at 2:04 am

    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).

    Reply
    • Pronay says

      November 23, 2020 at 11:40 pm

      Really it worked like charm !

      Reply
  4. Agustin says

    June 17, 2015 at 7:24 am

    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!

    Reply
    • JiveDig says

      June 17, 2015 at 9:01 am

      Great! Thanks for stopping by 😉

      Reply
    • Gabriel says

      November 28, 2020 at 5:59 pm

      great add, thumb up!
      thank you both for this snippet, after 5 years still works like a charm.

      Reply
  5. Matias says

    July 4, 2015 at 9:30 am

    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.

    Reply
    • JiveDig says

      July 4, 2015 at 10:31 am

      Whoa!!!! Does that really work? Will have to try. Super lean! Thanks.

      Reply
      • Piotr says

        January 12, 2017 at 9:23 am

        yeah! works great this way, thanks to both of you! 🙂

        Reply
    • DEr says

      August 17, 2015 at 6:02 am

      With function (tsm_convert_id_to_term_in_query) didnt work – shows no post on selected category.

      With ‘value_field’ => ‘slug’,
      works well

      Reply
      • JiveDig says

        August 17, 2015 at 10:13 am

        Thanks for confirmation. Looks like Matias has the more efficient version. I’ll have to update the snippet after I test.

        Reply
        • Pradhumn says

          August 25, 2021 at 3:52 pm

          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

          Reply
    • Deepak Ji says

      October 10, 2017 at 7:47 am

      Great, Thanks 😀

      Reply
  6. JS says

    July 14, 2015 at 9:37 am

    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?

    Reply
    • JiveDig says

      July 14, 2015 at 9:43 am

      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?

      Reply
    • Rob says

      June 3, 2017 at 4:48 am

      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.

      Reply
  7. Rob says

    September 10, 2015 at 6:33 am

    How can i add this with multiple custom taxonomies?

    Reply
    • JiveDig says

      September 22, 2015 at 2:42 pm

      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.

      Reply
    • Luko Del Ponte says

      November 11, 2015 at 7:38 am

      You will need to try with different approach. Something like this:
      https://pippinsplugins.com/post-list-filters-for-custom-taxonomies-in-manage-posts/

      Reply
  8. Mike says

    September 22, 2015 at 6:16 pm

    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!

    Reply
    • JiveDig says

      September 23, 2015 at 10:59 am

      And you’re sure the post_type and taxonomy names are correct?

      Reply
  9. Francesco says

    September 29, 2015 at 10:27 am

    Simple, easy, it works very well. Thanks!

    Reply
    • JiveDig says

      September 29, 2015 at 11:30 am

      Great!

      Reply
  10. Joel Bratsch says

    October 13, 2015 at 12:38 pm

    Sweet! Thanks for this post! Worked like a charm…

    Reply
    • JiveDig says

      October 13, 2015 at 12:39 pm

      Woot! Glad it helped 😉

      Reply
  11. Marek says

    October 15, 2015 at 11:40 am

    Excellent! Thanks a lot.
    It’d be great to have something like this for media.

    Reply
    • JiveDig says

      November 11, 2015 at 8:57 am

      JJJ recently released something awesome for this. WP Media Categories

      Reply
  12. acy says

    November 10, 2015 at 11:42 am

    Awesome! But doesn’t work with current media uploader… Any ideas?

    Reply
    • JiveDig says

      November 11, 2015 at 8:46 am

      What does a taxonomy filter have to do with the media uploader? Or, am I missing something here?

      Reply
  13. Darto KLoning says

    November 15, 2015 at 9:10 pm

    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.

    Reply
    • JiveDig says

      November 16, 2015 at 9:05 am

      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.

      Reply
  14. freelance web designer kerala says

    February 18, 2016 at 6:34 am

    Thanks …. 🙂

    Reply
    • JiveDig says

      February 18, 2016 at 10:30 am

      Cheers 😉

      Reply
  15. amrish pandey says

    April 9, 2016 at 4:20 pm

    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.

    Reply
  16. amrish pandey says

    April 9, 2016 at 4:32 pm

    ok never mind found it, its at the last of functions.php

    Reply
  17. tobias says

    May 4, 2016 at 3:13 am

    possible to create a plugin out of it?

    Reply
    • JiveDig says

      May 4, 2016 at 9:52 am

      You certainly could. I often put this type of code in custom site functionality plugins. You could do it quickly with something like Pluginception.

      Reply
  18. Tim says

    May 6, 2016 at 8:55 pm

    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”

    Reply
    • JiveDig says

      May 7, 2016 at 2:04 pm

      That sounds like you have something else going on. Did you try with other plugins deactivated? Maybe with a different theme too?

      Reply
    • Kirby Kalbaugh says

      October 20, 2016 at 4:19 pm

      I’m having the same problem. Filter only works once this does the same thing.

      Reply
  19. Robert says

    June 7, 2016 at 8:13 am

    This is gold!

    Reply
  20. Mark says

    July 15, 2016 at 9:17 am

    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.

    Reply
    • JiveDig says

      July 15, 2016 at 9:25 am

      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!

      Reply
  21. Mark says

    July 15, 2016 at 12:33 pm

    It was my fault. Problem was $query->set( ‘post_type’ …… added into another part of code.

    Reply
  22. Phillip says

    July 18, 2016 at 12:20 pm

    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.

    Reply
    • JiveDig says

      August 23, 2016 at 3:46 pm

      Very interesting point, thanks for this!

      Reply
    • Danielle says

      July 28, 2017 at 6:28 pm

      I know it’s been a year, but this saved me. Thank you!!

      Reply
  23. Lisa says

    August 20, 2016 at 12:39 pm

    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?

    Reply
    • JiveDig says

      August 23, 2016 at 3:44 pm

      There is always a way! But sadly, not one that I have tried to figure out yet 😉 Sorry!

      Reply
  24. Daron says

    August 24, 2016 at 1:36 pm

    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.

    Reply
  25. Sneha says

    September 13, 2016 at 6:01 am

    This works !! Thanks for the great tut, JiveDig.

    Reply
  26. loveitgroup says

    October 4, 2016 at 4:02 pm

    Great job. Very useful!
    Thnx!

    Reply
  27. Gabriel says

    January 19, 2017 at 9:10 pm

    Much appreciated, Mike!

    Reply
    • JiveDig says

      February 4, 2017 at 8:30 pm

      Woot!

      Reply
  28. Robert Huska says

    February 27, 2017 at 9:35 am

    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.

    Reply
  29. Karthik says

    May 9, 2017 at 3:34 pm

    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!

    Reply
  30. 3nokor says

    June 14, 2017 at 6:48 am

    this post help me more it’s very useful.
    Thanks you.

    Reply
  31. Mark Howells-Mead says

    July 12, 2017 at 7:35 am

    is_numeric($q_vars[$taxonomy]) returns true for “0” (string). Better to check as (int) $q_vars[$taxonomy].

    Reply
  32. wordpress taxonomy says

    July 25, 2017 at 8:15 am

    Great work and excellent post! This is really helpful to used my business site development.

    Thanks.

    Reply
  33. jean-charles says

    July 28, 2017 at 8:04 am

    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

    Reply
    • jean-charles says

      July 28, 2017 at 8:42 am

      I’ve found a way to do what i needed,

      just replace the taxonomy by the custom field slug

      and it works 🙂

      Reply
  34. Sébastien says

    August 4, 2017 at 11:56 am

    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 !

    Reply
  35. Joana says

    August 7, 2017 at 7:54 am

    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?

    Reply
    • JiveDig says

      December 5, 2017 at 9:42 am

      I use FacetWP all the time, but it doesn’t create taxonomies. FacetWP only uses taxonomies that already exist.

      Reply
  36. Rajesh says

    September 14, 2017 at 9:23 am

    Thanks for the provided information. Saved a lot of time..Cheers….

    Reply
  37. Mark Law says

    October 4, 2017 at 6:38 am

    Thanks, this is very useful indeed! 🙂

    Reply
  38. NikkaG says

    October 28, 2017 at 1:45 pm

    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

    Reply
  39. Darshan Saroya says

    December 21, 2017 at 1:12 am

    What if I want to filter custom post via post Meta?

    Reply
  40. Brendan Lawton says

    January 23, 2018 at 7:13 am

    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;
    }
    }

    Reply
  41. Josh F says

    March 20, 2018 at 11:28 am

    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

    Reply
  42. KIJO says

    March 22, 2018 at 8:53 am

    Very useful. Will be featuring this snippet in a WordPress help series.

    Reply
  43. Luke says

    June 20, 2018 at 1:43 am

    How can i remove the count from parent ?

    Reply
    • JiveDig says

      June 20, 2018 at 10:24 am

      It would think changing ‘show_count’ to false would be the first thing to try.

      Reply
  44. narayan says

    July 3, 2018 at 9:14 am

    This is greate

    Reply
  45. cris says

    September 18, 2018 at 4:51 pm

    thanks

    Reply
  46. Agence Debord says

    December 18, 2018 at 3:08 am

    Great post, very useful indeed!
    Tanks a lot

    Reply
  47. Rick says

    January 5, 2019 at 5:44 pm

    Brilliant! This was extremely helpful to me. Thanks for sharing!

    Reply
  48. globdug says

    March 28, 2019 at 4:12 am

    Thank you very much! Very useful!!! 🙂

    Reply
  49. janw.oostendorp says

    July 31, 2019 at 4:24 am

    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 )`

    Reply
    • JiveDig says

      July 31, 2019 at 10:21 am

      Great call. Snippet updated. Thanks!

      Reply
  50. Karthik says

    August 11, 2019 at 1:00 am

    Works perfectly! Thanks a ton for this post Mike!

    Reply
  51. umehass says

    September 2, 2019 at 7:40 pm

    Work like magic, thank you

    Reply
  52. FileMoro says

    November 20, 2019 at 12:10 am

    Nice work

    Reply
  53. Tejus says

    November 29, 2019 at 2:01 pm

    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.

    Reply
  54. Anjana says

    February 22, 2020 at 7:57 am

    Hello
    Instead of displaying drop-down in back-end is there any way to show in frontend

    Reply
  55. Amelia Jones says

    March 19, 2020 at 5:19 am

    Woow Its work for me, Thanks!

    Reply
  56. Duncan Michael MacGregor says

    July 10, 2020 at 7:32 am

    This saved me lots of hassle, thank you so much for this code! Works perfectly.

    Reply
  57. Luigi says

    August 27, 2020 at 4:33 am

    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

    Reply
  58. Sumit Thakur says

    January 15, 2021 at 1:18 am

    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

    Reply
  59. gianmarco says

    February 9, 2021 at 8:50 pm

    Where in wordpress do I insert this snippet? thanks

    Reply
    • JiveDig says

      June 3, 2021 at 12:12 pm

      The typical places are in functions.php of your theme, a custom functionality plugin, or a Code Snippets plugin.

      Reply
  60. joseph says

    February 15, 2021 at 10:01 pm

    I’m sorry, where the snippet?

    Reply
    • JiveDig says

      June 3, 2021 at 12:09 pm

      GitHub must’ve glitched. It’s there now.

      Reply
  61. Mohit says

    April 9, 2021 at 9:06 am

    Worked like wow!!

    Reply
  62. Edgardo says

    June 5, 2021 at 6:09 pm

    Beautiful script!
    How could it be modified to show “only” the parent categories?
    Thanks.

    Reply
  63. Flavio says

    June 11, 2021 at 10:37 am

    Thank you JiveDig, a real “plug and play” code!

    Reply
  64. Allan says

    July 1, 2021 at 11:44 am

    WOW!!! great snippet!! thank you so mucho. It works great.

    Reply
  65. Mack says

    January 4, 2022 at 3:49 pm

    Perfect! Thanks for sharing this. Saved me a lot of time.

    Reply
  66. Stefan says

    February 7, 2022 at 2:08 pm

    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!

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Our posts, directly to your inbox


Howdy, I'm Mike Hemberger. I like to build nice websites and web apps with WordPress and Genesis Framework.

I'm passionate about everything I do, and it resonates in all of my work.

Feel free to email, and you'll be speaking directly with me.

Recent Posts

Restful P2P Example

Create Posts 2 Posts ‘connections’ with the WordPress Rest API

EA Share Count SMS button

Add SMS share button to EA Share Count plugin

ACF Extras

ACF Extras officially launched! A community of code snippets and field groups

Remove Items from TinyMCE editor

Remove Buttons/Items From The WordPress TinyMCE Editor

  • My Account
  • Affiliate Area
  • Log In|Log Out
  • GitHub
  • Instagram
  • Twitter
  • YouTube

Copyright © 2025 · The Stiz Media, LLC · All Rights Reserved