Filtering by Taxonomies
Retrieving filtered data with
WP_Query and other supported query classes is a no brainier. But for advanced filtering we may need to retrieve data assigned to a specific multiple categories, tags or any other taxonomy terms.
WordPress stores taxonomy-related data in separate database tables.
wp_terms holds the term data.
wp_term_relationships stores the relationships between terms (category, tag, custom) and an object (mostly posts but can be used for other objects in WordPress – like Users or Comments).
wp_term_taxonomy describes the taxonomy (category, link or tag) for the entries in the
To retrieve data from those tables it translates query data and conditions to SQL. But you shouldn’t write your own SQL – it’s a bad practice! WordPress will do it for you, in a secure way, with caching and other mechanisms that ensure maximum performance and utilization.
WP_Query class retrieves taxonomy-related data using the
tax_query parameter, which accepts an array. This array is handled by the
WP_Tax_Query class to generate an SQL clause which in return retrieves the needed data.
In theory, we can assign taxonomies to not only Posts, but also to Comments and Users, and then filter them using the
WP_Tax_Query class. But this topic is way out of the scope of this post. Maybe in the future I will write a separate post about this, let me know in the comments if you’re interested and I’ll take that into account!
Introducing The Tax Query Generator
The WP_Tax_Query Generator is designed to help you create custom taxonomy queries, save them to your account and integrate into the main WP_Query. Use this new generator to learn the query internals, or use it as part of your daily, WordPress development workflow. You can create simple queries or even multiple advanced queries with different kind of relations between them.
This generator also supports nested queries, allowing you to combine several taxonomy queries inside each other to retrieve very complex queries.
In this example, we are going to filter posts in a news website with tens of thousands of posts. Let’s start with a simple example – filtering posts from the spaceflight category.
Now that we got that down, let’s filter results from multiple taxonomies. We are going to filter posts from the spaceflight category AND from the opinion section AND a video/audio Post Format. We will use the
relation operator to match all filters/conditions.
Let’s create a complex filter with nested items. We are going to retrieve the above query OR all the items tagged with Nasa and Space tags.
Finally, we need to insert the filter settings into our
WP_Query query. We will use the $tax query array generated above, inside the
WordPress offers many built in tools. As in the case of the Tax Query, most of these tools are very simple to learn and offer advanced capabilities. Many of those capabilities are already integrated into our generators. We hope our tools serve your needs well!
Feel free to let us know what you think about the Tax Query in the comments.