WP Query Block Extension – Override Taxonomy Filter Operand
The Query Loop Block within WordPress allows you to search in the database for content on your site and display the results of that query. You’re able to apply filters to your search query meaning you can restrict the results of the search query. One of the native filters that WordPress provides is the Taxonomy filter. The taxonomy filter allows you to restrict the results of your search query to content that you have categorized or tagged in certain ways.
By default, if you add more than one category or tag (technically: taxonomy terms), WordPress will show results that match ANY of those taxonomy terms. i.e. if you add a taxonomy filter and select 2 of your site’s categories, WordPress will show posts (or pages, with the ‘Taxonomy for Pages’ plugin) that are in either of those posts.
Sometimes, however, it might be more beneficial to have a database query that allows you to say “I only want to show posts that are specifically in BOTH of these categories”. That’s where the WP Query Block Extension – Override Taxonomy Filter plugin comes in.
Using the Block Extension – Override Taxonomy Filter Operand plugin
Activate the plugin from the plugins menu in your dashboard. Once activated, the ability for you to choose the operand – the way the search query chooses between ‘any’ or ‘both’ in the example above – becomes available to you. This will not impact any of your existing query loops on your site.
Add a query loop block to a post or page (or edit an existing one) and select the three vertical dots next to ‘Filters’. Select ‘Taxonomies’.
Once you add more than one category or tag new options for this query will appear which allow you to choose the type of query made in the database. A “Tax Query Operand” panel will be shown. When expanded, three options are available.
The ‘Outer Query Operand’ allows you to to determine if you want your query to show only content that is attached to all of the categories and tags you enter (‘AND’) or any of them (‘OR’). Technically this is the ‘relation’ parameter in the WP Query being made.
There are also drop-downs for both categories and tags that allow you to adjust the database query specific to each taxonomy. These drop-downs contain more options: IN, NOT IN, AND, EXISTS, NOT EXISTS.
IN: Include all posts/pages that are attached to any of the listed taxonomy terms
NOT IN: Include all posts/pages that are NOT attached any of the listed taxonomy terms
AND: Include only posts which are attached to ALL of the listed taxonomy terms
EXISTS: Include posts/pages that have any terms in the taxonomy regardless of what is entered in the terms list. (i.e. any post which has absolutely any category at all)
NOT EXISTS: Include posts/pages that don’t have any term at all. i.e. “orphaned” content.
Practical Examples
I want to show only posts that are attached to both the ‘Events’ category AND ‘learningtech’ tag. (i.e. a post must be in the Events category as well as be tagged with learningtech for it to show)
- Categories: Events
- Tags: learningtech
- Outer Query Operand: AND
- Category Query Operand: IN
- Post Tag Query Operand: IN
I want to show posts that are in the ‘Events’ category but are not tagged with ‘2024’:
- Categories: Events
- Tags: 2024
- Outer Query Operand: AND
- Category Query Operand: IN
- Post Tag Query Operand: NOT IN
I want to show all posts that do not have any categories and tags
- Categories: any
- Tags: any
- Outer Query Operand: AND
- Category Query Operand: NOT EXISTS
- Post Tag Query Operand: NOT EXISTS
