Blog

Filter Posts by Taxonomies

Filtering Posts by Taxonomies in the Dashboard

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.

 

Filter WordPress Posts by Category
Filter WordPress Posts by Category

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

 

Cars Admin Screen
Cars Admin Screen

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.

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.

 

Filter Cars by Taxonomy
Filter Cars by Taxonomy

Hope this cool solution will help your clients manage their content!

Rami Yushuvaev

I'm an entrepreneur, a web developer and a blogger. I’ve contributed code to each and every release since WordPress 2.8. I'm the Hebrew GTE responsible for the translation and the release of WordPress Hebrew version. The founder GenerateWP.com and several other WordPress related projects. I work mainly with Israeli startups, providing plugin development services.

5 Comments:

Leave a Reply