By default, the admin Posts screen allows us to filter posts by the built-in “Categories” Taxonomy, using a neat, user-friendly dropdown menu. But in many cases, when we register new Taxonomies we often want to be able to filter posts by these new Taxonomies as well. In this quick tutorial, we will learn how to add new filters (dropdowns) to any Post Type screen in order to filter content by custom Taxonomies.
Registering a New Post Type and Taxonomies
For many WordPress developers this is a basic step, we do it in almost every project. Therefore, we won’t be getting into how to set it all up in your code — instead, use the following links to navigate to the snippets. You can use the Post Type Generator and the Taxonomy Generator to do that.
Ok, so in our example we will create a car catalog for an imaginary car retailer or agency. We will create a new “Car” post type and add several taxonomies (manufacturer, model, transmission, doors and color).
Adding Filters to the Post Type Admin Screen
Now that we have a working “Car” Post Type with several Taxonomies, we would like to be able to filter our cars in the admin area. For that we need to add filters (dropdowns) to help us sort our content and manage it easily.
By default, when we register new Taxonomies, WordPress does not add sorting filters to the admin area screen. However, developers are capable of doing that manually using the restrict_manage_posts
action which fires just before the “Filter” button is printed out.
Upgrade to GenerateWP Premium Enjoy better WordPress generators Upgrade Now
This much useful action has two parameters: The $post_type
parameter accepts the Post Type’s slug and the $which
parameter accepts the location of the navigation markup: ‘top’, ‘bottom’ (it also allows ‘bar’ for the Media screen).
For our example we will use the $post_type
parameter to apply these filters only in the “Car” admin screen.
The Result
Now we have Taxonomy filters in our Cars admin screen, which allows us to display content that is assigned to the currently user-selected Taxonomies.
Hope this cool solution will help your clients manage their content!
19 Comments:
barbareshet
Great feature, and a great tutorial. thanks
Sébastien
Hi Rami !
Great article, very clear !
My approach is a little bit different but same result..
Interested in sorting the Posts or Custom Posts types by a taxonomy column ? Please have look at this little plugin http://www.concepteur-developpeur-web.fr/plugin-wordpress-filtres-colonnes-taxonomies-personnalisees/
Still, very good job 😉
Rami Yushuvaev
Hi Sebastian,
If you are not a coder, this a very nice plugin!
(Direct link to the plugin page in wp.org: https://wordpress.org/plugins/custom-taxonomies-filters-columns/)
Dima Minka
Nice post, thx Rami
Jojogali
Hi! Cool tutorial, easy to follow. Helped me a lot 😀 I was wondering if it was possible to do the same with user taxonomy? I’ve created a custom taxonomy for users and would like to be able to filter the posts by that taxonomy, so I only see posts created by User with X characteristic or Y.
Josh F
Hi! great post! Im new to wordpress and this is really helpful if i can use it! It would be greatly appreciated if you can direct me to where i can paste this php code and what areas in the code I should change to accomodate it to my post?
Antti Koskinen
Hi,
Thank you for your example. I modified your code to fit my needs and to add filtering to multiple post types. You can check my code on Github, https://gist.github.com/koskinenaa/86fd3acd75959e9aa0635ce00934e26c
Regards,
Antti
blogtjrotj
thank you
Eduardo Weidman Barijan
Thank you, it works! Easy to follow and implement in custom theme.
Amara Irum
great , can you please tell me how to display the filter on fronted using custom taxonomy ?
janasieber
Works, thank you! Saved me a lot of time 🙂
erikkubicazm
Thank you, saved my time. Can I somehow buy you a beer / coffee?
brennanpdx
Thank you!
kel
Hey Rami, you got me going in the right direction but I found an easier solution that I think you would be interested in. WordPress has a function that creates the dropdown called wp_dropdown_categories used by posts. This allows you replace all of your code in the foreach with this:
wp_dropdown_categories([
‘show_option_all’ => get_taxonomy($taxonomy_slug)->labels->all_items,
‘hide_empty’ => 0,
‘hierarchical’ => 1,
‘show_count’ => 0,
‘orderby’ => ‘name’,
‘name’ => $taxonomy_slug,
‘value_field’ => ‘slug’,
‘taxonomy’ => $taxonomy_slug,
]);
esyaalanyer
thank you, but not woking wc 3.6.2
moderndayfreak
This isn’t working for me either. We used CPT UI for the custom taxonomies.
closemarketing
Excelent article!
julesallamigeon
Hi, how can i display only one custom field on my dashboard admin ?
regards
thiagolamosa
I’m having the same problem.
I select the options in the dropdown and press the filter button, but it’s not filtering the posts… I also used CPT UI for the custom taxonomies.