Meta queries help you find posts or items based on extra information stored with them. This lets you filter content easily.
Meta queries for filtering in Wordpress
'meta_query' => [ [ 'key' => 'meta_key_name', 'value' => 'value_to_compare', 'compare' => '=', 'type' => 'CHAR' ], // More meta query arrays can be added ]
key is the meta field name you want to filter by.
value is what you want to match or compare.
$args = [ 'post_type' => 'product', 'meta_query' => [ [ 'key' => 'color', 'value' => 'blue', 'compare' => '=' ] ] ];
$args = [ 'meta_query' => [ [ 'key' => 'price', 'value' => [10, 50], 'compare' => 'BETWEEN', 'type' => 'NUMERIC' ] ] ];
$args = [ 'meta_query' => [ 'relation' => 'AND', [ 'key' => 'color', 'value' => 'red', 'compare' => '=' ], [ 'key' => 'size', 'value' => 'large', 'compare' => '=' ] ] ];
This code finds all 'product' posts where the meta field 'color' equals 'green'. It prints the titles of those products or a message if none are found.
<?php $args = [ 'post_type' => 'product', 'meta_query' => [ [ 'key' => 'color', 'value' => 'green', '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 products found with color green.\n"; } wp_reset_postdata();
Always set the type in meta queries when comparing numbers or dates to avoid wrong results.
You can combine multiple meta queries using relation with 'AND' or 'OR'.
Use wp_reset_postdata() after custom queries to restore global post data.
Meta queries filter posts by custom field values.
You can compare exact values, ranges, or multiple conditions.
They help create dynamic and precise content filters in WordPress.
Practice
meta_query in a WordPress query?Solution
Step 1: Understand what
meta_querydoesmeta_queryis used to filter posts by their custom fields (meta data).Step 2: Compare with other query parameters
Changing post type, sorting by date, or limiting posts are done by other parameters, notmeta_query.Final Answer:
To filter posts based on custom field values -> Option CQuick Check:
Meta query = filter by custom fields [OK]
- Confusing meta_query with post_type parameter
- Thinking meta_query sorts posts
- Assuming meta_query limits posts count
meta_query?Solution
Step 1: Check the structure of
meta_querymeta_queryexpects an array of arrays, each with 'key', 'value', and optional 'compare'.Step 2: Verify the comparison operator
To check exact equality, 'compare' should be '='. "meta_query" => array(array('key' => 'color', 'value' => 'blue', 'compare' => '=')) matches this exactly.Final Answer:
"meta_query" => array(array('key' => 'color', 'value' => 'blue', 'compare' => '=')) -> Option AQuick Check:
Exact match uses compare '=' inside nested array [OK]
- Omitting the nested array inside meta_query
- Using wrong compare operator like '!=' or 'LIKE' for exact match
- Passing meta_query as a single array without nesting
meta_query in a WP_Query, what posts will be returned?"meta_query" => array(
array(
'key' => 'price',
'value' => array(10, 50),
'type' => 'NUMERIC',
'compare' => 'BETWEEN'
)
)Solution
Step 1: Understand the 'BETWEEN' compare
'BETWEEN' selects values within the range including boundaries.Step 2: Check the value array and type
Value array is [10, 50] and type is 'NUMERIC', so numeric comparison between 10 and 50 applies.Final Answer:
Posts with 'price' between 10 and 50 inclusive -> Option DQuick Check:
BETWEEN with numeric type = range filter [OK]
- Thinking BETWEEN excludes boundaries
- Confusing BETWEEN with NOT BETWEEN
- Ignoring 'type' => 'NUMERIC' and treating as string
meta_query that tries to filter posts with 'rating' greater than 4:"meta_query" => array( 'key' => 'rating', 'value' => 4, 'compare' => '>' )
Solution
Step 1: Check meta_query structure
meta_querymust be an array of arrays, but here it is a single array without nesting.Step 2: Confirm other parts are correct
Compare '>' is valid, value can be a single number, and 'type' is optional but recommended.Final Answer:
Missing nested array inside meta_query -> Option BQuick Check:
meta_query needs nested arrays [OK]
- Passing a single array instead of array of arrays
- Assuming 'type' is mandatory (it's optional but recommended)
- Using wrong compare operator syntax
meta_query setup correctly achieves this?Solution
Step 1: Understand filtering for multiple values
To filter posts where 'status' is one of multiple values, use 'value' as an array and 'compare' as 'IN'.Step 2: Check options for correct usage
"meta_query" => array( array('key' => 'status', 'value' => array('published', 'archived'), 'compare' => 'IN') ) uses 'value' => array('published', 'archived') and 'compare' => 'IN', which is the correct pattern.Final Answer:
"meta_query" => array( array('key' => 'status', 'value' => array('published', 'archived'), 'compare' => 'IN') ) -> Option AQuick Check:
Use 'IN' with array value for multiple matches [OK]
- Using 'OR' relation with separate arrays instead of 'IN'
- Using 'AND' relation which requires both values simultaneously
- Passing comma-separated string instead of array for multiple values
Ready to prove it?
Pass the test and this pattern moves unknown
