Tax queries and meta queries help you find posts based on categories, tags, or custom fields. They make your website show exactly what visitors want to see.
Tax queries and meta queries in Wordpress
$query = new WP_Query([ 'tax_query' => [ [ 'taxonomy' => 'category', 'field' => 'slug', 'terms' => ['news', 'updates'], 'operator' => 'IN' ] ], 'meta_query' => [ [ 'key' => 'price', 'value' => 100, 'compare' => '>=', 'type' => 'NUMERIC' ] ] ]);
tax_query filters posts by taxonomy terms like categories or tags.
meta_query filters posts by custom fields (meta data) like price or color.
$query = new WP_Query([ 'tax_query' => [ [ 'taxonomy' => 'category', 'field' => 'slug', 'terms' => ['news'] ] ] ]);
$query = new WP_Query([ 'meta_query' => [ [ 'key' => 'color', 'value' => 'blue', 'compare' => '=' ] ] ]);
$query = new WP_Query([ 'tax_query' => [ [ 'taxonomy' => 'post_tag', 'field' => 'slug', 'terms' => ['featured'] ] ], 'meta_query' => [ [ 'key' => 'rating', 'value' => 4, 'compare' => '>=' ] ] ]);
This code finds posts in the 'news' category that have a custom field 'featured' set to 'yes'. It prints the titles of these posts or says no posts found.
<?php $args = [ 'post_type' => 'post', 'tax_query' => [ [ 'taxonomy' => 'category', 'field' => 'slug', 'terms' => ['news'] ] ], 'meta_query' => [ [ 'key' => 'featured', 'value' => 'yes', 'compare' => '=' ] ] ]; $query = new WP_Query($args); if ($query->have_posts()) { while ($query->have_posts()) { $query->the_post(); echo get_the_title() . "\n"; } } else { echo "No posts found.\n"; } wp_reset_postdata(); ?>
Always use wp_reset_postdata() after custom queries to avoid conflicts.
You can combine multiple taxonomies and meta queries using arrays for complex filters.
Use operator in tax queries to control how terms are matched (e.g., IN, NOT IN).
Tax queries filter posts by categories, tags, or custom taxonomies.
Meta queries filter posts by custom fields (meta data).
Combining tax and meta queries helps create precise post filters.
Practice
tax_query in WordPress?Solution
Step 1: Understand tax_query purpose
Atax_queryis used to filter posts by taxonomy terms like categories or tags.Step 2: Compare with meta_query
Ameta_queryfilters by custom fields, not taxonomies.Final Answer:
It filters posts based on categories, tags, or custom taxonomies. -> Option CQuick Check:
tax_query = taxonomy filter [OK]
- Confusing tax_query with meta_query
- Thinking tax_query sorts posts
- Assuming tax_query updates data
Solution
Step 1: Identify meta_query structure
A meta query uses 'key', 'value', and 'compare' keys to filter custom fields.Step 2: Match exact equality
To check exact match, 'compare' should be '=' and 'value' a string, not array.Final Answer:
meta_query => [ ['key' => 'color', 'value' => 'blue', 'compare' => '='] ] -> Option BQuick Check:
Exact match uses compare '=' [OK]
- Using tax_query instead of meta_query
- Using 'LIKE' when exact match needed
- Passing value as array when not needed
query_posts([
'tax_query' => [[
'taxonomy' => 'category',
'field' => 'slug',
'terms' => ['news', 'updates'],
'operator' => 'AND'
]],
'meta_query' => [[
'key' => 'featured',
'value' => '1',
'compare' => '='
]]
]);
Solution
Step 1: Analyze tax_query operator
Operator 'AND' means posts must be in both 'news' and 'updates' categories simultaneously.Step 2: Analyze meta_query condition
Meta query filters posts where 'featured' meta key equals '1'.Final Answer:
Posts in both 'news' AND 'updates' categories with meta 'featured' = '1'. -> Option DQuick Check:
tax_query 'AND' + meta_query '=' = Posts in both 'news' AND 'updates' categories with meta 'featured' = '1'. [OK]
- Confusing 'AND' with 'OR' operator
- Ignoring meta_query filter
- Assuming syntax error incorrectly
$args = [
'tax_query' => [
'taxonomy' => 'genre',
'field' => 'slug',
'terms' => 'fiction'
]
];
$query = new WP_Query($args);
Solution
Step 1: Check tax_query structure
tax_query expects an array of arrays, each inner array defines one taxonomy filter.Step 2: Identify the provided structure
Here tax_query is a single associative array, missing the outer array wrapping.Final Answer:
tax_query must be an array of arrays, but here it's a single array. -> Option AQuick Check:
tax_query = array of arrays [OK]
- Passing single array instead of array of arrays
- Thinking 'terms' must always be array
- Ignoring tax_query structure rules
Solution
Step 1: Understand combining queries
tax_query and meta_query are combined with AND logic by default when both are included in the query args.Step 2: Correct structure
Simply place both 'tax_query' and 'meta_query' as keys in the main query args array; no 'relation' is needed.Final Answer:
Combine tax_query and meta_query inside a single array without 'relation'. -> Option AQuick Check:
tax_query + meta_query default = AND [OK]
- Adding unnecessary 'relation' => 'AND'
- Nesting queries inside one another
- Using 'relation' => 'OR' for AND logic
Ready to prove it?
Pass the test and this pattern moves unknown
