In this third part of our series, after presenting the WP_Meta_Query and the WP_Date_Query Generators, we are introducing the WP_Tax_Query Generator which allows you to filter queries by taxonomies.
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 wp_terms
table.
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.
The 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.
Examples
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.
Upgrade to GenerateWP Premium Enjoy better WordPress generators Upgrade Now
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 tax_query
argument.
In Conclusion
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.
2 Comments:
Omer Ben Shushan
Rami you always keep on innovating.. thanks for giving out such value in such a professional way as always.. just a little question – will there be any more WordPress generators in this series of WP_Query classes?
I find them very useful so thanks for that.
Rami Yushuvaev
Hi Omer, thank you for the kind words.
To answer your question, this is the last “Query” generator. Now I will be focusing on the Settings API Generator and few other generators in my TODO list.