Bird
Raised Fist0
Wordpressframework~5 mins

Meta queries for filtering in Wordpress

Choose your learning style10 modes available
Introduction

Meta queries help you find posts or items based on extra information stored with them. This lets you filter content easily.

You want to show posts with a specific custom field value.
You need to filter products by price or color stored as metadata.
You want to find events happening on a certain date saved in meta.
You want to combine multiple meta conditions to narrow results.
You want to exclude posts with certain meta values.
Syntax
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.

Examples
Find products where the 'color' meta field is exactly 'blue'.
Wordpress
$args = [
  'post_type' => 'product',
  'meta_query' => [
    [
      'key' => 'color',
      'value' => 'blue',
      'compare' => '='
    ]
  ]
];
Find posts with 'price' meta between 10 and 50.
Wordpress
$args = [
  'meta_query' => [
    [
      'key' => 'price',
      'value' => [10, 50],
      'compare' => 'BETWEEN',
      'type' => 'NUMERIC'
    ]
  ]
];
Find posts where 'color' is 'red' AND 'size' is 'large'.
Wordpress
$args = [
  'meta_query' => [
    'relation' => 'AND',
    [
      'key' => 'color',
      'value' => 'red',
      'compare' => '='
    ],
    [
      'key' => 'size',
      'value' => 'large',
      'compare' => '='
    ]
  ]
];
Sample Program

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.

Wordpress
<?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();
OutputSuccess
Important Notes

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.

Summary

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

(1/5)
1. What is the main purpose of using meta_query in a WordPress query?
easy
A. To sort posts by date
B. To change the post type
C. To filter posts based on custom field values
D. To limit the number of posts returned

Solution

  1. Step 1: Understand what meta_query does

    meta_query is used to filter posts by their custom fields (meta data).
  2. Step 2: Compare with other query parameters

    Changing post type, sorting by date, or limiting posts are done by other parameters, not meta_query.
  3. Final Answer:

    To filter posts based on custom field values -> Option C
  4. Quick Check:

    Meta query = filter by custom fields [OK]
Hint: Meta queries filter posts by custom fields only [OK]
Common Mistakes:
  • Confusing meta_query with post_type parameter
  • Thinking meta_query sorts posts
  • Assuming meta_query limits posts count
2. Which of the following is the correct syntax to filter posts where the custom field 'color' equals 'blue' using meta_query?
easy
A. "meta_query" => array(array('key' => 'color', 'value' => 'blue', 'compare' => '='))
B. "meta_query" => array('key' => 'color', 'value' => 'blue')
C. "meta_query" => array(array('key' => 'color', 'value' => 'blue', 'compare' => 'LIKE'))
D. "meta_query" => array(array('key' => 'color', 'value' => 'blue', 'compare' => '!='))

Solution

  1. Step 1: Check the structure of meta_query

    meta_query expects an array of arrays, each with 'key', 'value', and optional 'compare'.
  2. 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.
  3. Final Answer:

    "meta_query" => array(array('key' => 'color', 'value' => 'blue', 'compare' => '=')) -> Option A
  4. Quick Check:

    Exact match uses compare '=' inside nested array [OK]
Hint: Use nested array with 'key', 'value', and 'compare' '=' [OK]
Common Mistakes:
  • 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
3. Given the following 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'
  )
)
medium
A. Posts with 'price' not set
B. Posts with 'price' less than 10 or greater than 50
C. Posts with 'price' exactly 10 or exactly 50 only
D. Posts with 'price' between 10 and 50 inclusive

Solution

  1. Step 1: Understand the 'BETWEEN' compare

    'BETWEEN' selects values within the range including boundaries.
  2. 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.
  3. Final Answer:

    Posts with 'price' between 10 and 50 inclusive -> Option D
  4. Quick Check:

    BETWEEN with numeric type = range filter [OK]
Hint: BETWEEN with numeric type filters inclusive range [OK]
Common Mistakes:
  • Thinking BETWEEN excludes boundaries
  • Confusing BETWEEN with NOT BETWEEN
  • Ignoring 'type' => 'NUMERIC' and treating as string
4. Identify the error in this meta_query that tries to filter posts with 'rating' greater than 4:
"meta_query" => array(
  'key' => 'rating',
  'value' => 4,
  'compare' => '>'
)
medium
A. Wrong compare operator for greater than
B. Missing nested array inside meta_query
C. Value should be an array, not a single number
D. Missing 'type' => 'NUMERIC' for numeric comparison

Solution

  1. Step 1: Check meta_query structure

    meta_query must be an array of arrays, but here it is a single array without nesting.
  2. Step 2: Confirm other parts are correct

    Compare '>' is valid, value can be a single number, and 'type' is optional but recommended.
  3. Final Answer:

    Missing nested array inside meta_query -> Option B
  4. Quick Check:

    meta_query needs nested arrays [OK]
Hint: Always nest conditions inside an array in meta_query [OK]
Common Mistakes:
  • Passing a single array instead of array of arrays
  • Assuming 'type' is mandatory (it's optional but recommended)
  • Using wrong compare operator syntax
5. You want to filter posts where the custom field 'status' is either 'published' or 'archived'. Which meta_query setup correctly achieves this?
hard
A. "meta_query" => array( array('key' => 'status', 'value' => array('published', 'archived'), 'compare' => 'IN') )
B. "meta_query" => array( array('key' => 'status', 'value' => 'published,archived', 'compare' => 'LIKE') )
C. "meta_query" => array( 'relation' => 'AND', array('key' => 'status', 'value' => 'published'), array('key' => 'status', 'value' => 'archived') )
D. "meta_query" => array( array( 'relation' => 'OR', 'key' => 'status', 'value' => 'published', 'compare' => '=' ), array('key' => 'status', 'value' => 'archived', 'compare' => '=') )

Solution

  1. 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'.
  2. 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.
  3. Final Answer:

    "meta_query" => array( array('key' => 'status', 'value' => array('published', 'archived'), 'compare' => 'IN') ) -> Option A
  4. Quick Check:

    Use 'IN' with array value for multiple matches [OK]
Hint: Use 'compare' => 'IN' with array for multiple values [OK]
Common Mistakes:
  • 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
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard