📖 Scenario: You are building a WordPress site that lists books. You want to show only books that belong to a specific genre and have a custom field rating above a certain value.
🎯 Goal: Build a WordPress query that fetches posts of type book filtered by a taxonomy term genre and a meta field rating greater than 4.
📋 What You'll Learn
Create a taxonomy query filtering by the genre term 'fiction'
Create a meta query filtering books with rating greater than 4
Combine taxonomy and meta queries in a single WP_Query
Use correct array keys and structure for tax_query and meta_query
💡 Why This Matters
🌍 Real World
Filtering posts by categories and custom fields is common in WordPress sites like online stores, blogs, and portfolios.
💼 Career
Understanding tax queries and meta queries is essential for WordPress developers building custom themes and plugins that require advanced content filtering.
Progress0 / 4 steps
1
Create the base WP_Query arguments
Create an array called $args with a key post_type set to 'book'.
Wordpress
Hint
Start by specifying the post type you want to query.
2
Add a taxonomy query for genre 'fiction'
Add a tax_query key to $args with an array filtering the taxonomy 'genre' for the term 'fiction'. Use 'field' => 'slug' and 'terms' => 'fiction'.
Wordpress
Hint
Use a nested array for tax_query with the correct keys.
3
Add a meta query for rating greater than 4
Add a meta_query key to $args with an array filtering the meta key 'rating' for values greater than 4. Use 'compare' => '>' and 'type' => 'NUMERIC'.
Wordpress
Hint
Use a nested array for meta_query with the correct keys and numeric comparison.
4
Create the WP_Query with combined tax and meta queries
Create a variable $query and assign it a new WP_Query object using the $args array.
Wordpress
Hint
Use the WP_Query class with the arguments array to get the filtered posts.
Practice
(1/5)
1. Which of the following best describes a tax_query in WordPress?
easy
A. It filters posts based on custom field values.
B. It sorts posts by date or title.
C. It filters posts based on categories, tags, or custom taxonomies.
D. It updates post metadata values.
Solution
Step 1: Understand tax_query purpose
A tax_query is used to filter posts by taxonomy terms like categories or tags.
Step 2: Compare with meta_query
A meta_query filters by custom fields, not taxonomies.
Final Answer:
It filters posts based on categories, tags, or custom taxonomies. -> Option C
Quick Check:
tax_query = taxonomy filter [OK]
Hint: Tax queries filter by taxonomy terms like categories [OK]
Common Mistakes:
Confusing tax_query with meta_query
Thinking tax_query sorts posts
Assuming tax_query updates data
2. Which of the following is the correct syntax to create a meta query filtering posts where the meta key 'color' equals 'blue'?
5. You want to query posts that are in the 'events' category AND have a custom field 'priority' set to 'high'. Which is the correct way to combine tax_query and meta_query to achieve this?
medium
A. Use 'relation' => 'AND' to combine tax_query and meta_query for AND behavior.
B. Nest tax_query and meta_query inside a 'relation' => 'OR' array within 'meta_query'.
C. Combine tax_query and meta_query inside a single array without 'relation'.
D. Use 'relation' => 'OR' at the top level combining tax_query and meta_query arrays.
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 A
Quick Check:
tax_query + meta_query default = AND [OK]
Hint: tax_query and meta_query default to AND logic [OK]
Common Mistakes:
Adding unnecessary 'relation' => 'AND'
Nesting queries inside one another
Using 'relation' => 'OR' for AND logic
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.