Troubleshooting
How to Fix a Meta Query in WordPress: Causes and Fixes

Short answer
To fix a WordPress meta query, confirm the exact meta key and stored value, set the right type for numbers and dates, and wrap every clause in an outer array. Then print the generated SQL or use Query Monitor to see what WordPress actually ran.
Key takeaways
- Meta keys must match exactly, including underscores and case.
- Numbers and dates need a type such as NUMERIC or DATE.
- Every clause needs its own array inside meta_query.
- Print the generated SQL to see what WordPress actually ran.
- Ordering by a meta key drops posts that lack that key.
Why is my WordPress meta query not working?
A WordPress meta query usually fails because the meta key or value does not match what is stored, the comparison type is wrong, or the array structure is nested incorrectly.
Meta queries filter posts by custom field values. They are powerful, but they fail quietly. A broken query rarely shows an error and simply returns no posts, the wrong posts, or every post.
That silence is what makes them frustrating. The fix is to stop guessing and look at exactly what is stored and what SQL WordPress builds.
This guide covers the most common mistakes, how to debug them and how to keep meta queries fast once they work.
How does a meta query work?
A meta query is an array you pass to WP_Query under the meta_query argument, and WordPress turns it into a SQL join against the postmeta table.
Each clause in the array has a key, an optional value, a compare operator and an optional type. Several clauses can be combined with a relation of AND or OR.
$query = new WP_Query( array(
'post_type' => 'product',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'price',
'value' => 50,
'compare' => '>=',
'type' => 'NUMERIC',
),
array(
'key' => 'in_stock',
'value' => '1',
),
),
) );
The full list of arguments is in the WP_Meta_Query class reference. The WP_Query reference shows how it fits with other query arguments.
If you are new to custom queries, our guide on how to use WP_Query covers the basics first.
Does the meta key match exactly?
The meta key must match the stored key exactly, including case, underscores and any prefix, or the query will return nothing.
Plugins often store values under keys you would not guess. A field labelled Price in a form may be saved as _price, product_price or field_63ab12.
Keys that start with an underscore are hidden from the Custom Fields panel, so you may not see them in the editor. WooCommerce, for example, stores the product price under _price.
To find the real key, run get_post_meta( $post_id ) on a known post and print the result, or look in the postmeta table with a database tool. Our guide on how to add custom fields in WordPress explains how these keys are created.
Is the stored value what you expect?
Meta values are stored as text, so the value in your query must match the stored format exactly, including spaces, case and number formatting.
A checkbox might be saved as 1, yes, on or true. A date might be 20261121, 2026-11-21 or a Unix timestamp. Each needs a different query.
Serialized arrays are a common trap. If a plugin saves several values in one field, the database holds a long string encoded with PHP serialize(), and an = comparison against one item will never match.
You can use LIKE as a rough workaround for serialized data, but it can match the wrong items. If you control the data, save each value as a separate meta row instead.
For Advanced Custom Fields users, the ACF documentation on querying posts by custom fields shows the storage format for each field type. Our guide on how to use Advanced Custom Fields covers setup.
Are you using the right compare operator and type?
Numbers and dates need the right type, because without it WordPress compares values as text and sorts 100 before 20.
By default, meta values are compared as strings. That works for exact matches but breaks greater-than and less-than checks on numbers.
| Data stored | Set type to | Example compare |
|---|---|---|
| Whole numbers | NUMERIC | >= |
| Prices with decimals | DECIMAL(10,2) | BETWEEN |
| Dates as YYYY-MM-DD | DATE | > |
| Dates as YYYYMMDD | DATE or NUMERIC | <= |
| Text | CHAR (default) | = or LIKE |
| Field exists at all | none | EXISTS or NOT EXISTS |
Operators like IN, NOT IN and BETWEEN need the value to be an array. BETWEEN needs exactly two items, the low value first.
EXISTS and NOT EXISTS do not need a value. They are useful for finding posts where a field was never set.
Is the meta_query array nested correctly?
The meta_query argument must be an array of clause arrays, so a single clause still needs an extra level of nesting.
This is one of the most common mistakes:
// Wrong: missing the outer array
'meta_query' => array(
'key' => 'color',
'value' => 'blue',
),
// Right
'meta_query' => array(
array(
'key' => 'color',
'value' => 'blue',
),
),
The relation key sits at the same level as the clauses, not inside one. Nested groups are allowed, so you can mix AND and OR, but each group needs its own relation.
If you only need one simple condition, the top-level meta_key and meta_value arguments are an easier option.
Why does ordering by a meta value not work?
Ordering by a meta value needs the orderby argument to point at the meta, and numeric fields need meta_value_num or a typed named clause.
The simplest form sets meta_key and 'orderby' => 'meta_value_num'. This sorts numerically, so 9 comes before 10.
For more control, give a meta query clause a name and order by that name:
'meta_query' => array(
'price_clause' => array(
'key' => 'price',
'type' => 'NUMERIC',
),
),
'orderby' => 'price_clause',
'order' => 'ASC',
Note that ordering by a meta key leaves out posts that do not have that key. If posts go missing, add an OR group with an EXISTS and a NOT EXISTS clause for the same key.
Is something else changing your query?
Yes, sometimes the meta query is fine but another argument, a plugin or a pre_get_posts filter changes the results.
Check the post_type and post_status arguments. Querying the default post type when your data is on a custom post type will always return nothing.
Pagination can hide results too. The default posts per page setting may be cutting the list short, so set posts_per_page explicitly while testing.
Plugins and themes can alter queries through the pre_get_posts action. Search your theme and custom plugins for it if the main query behaves differently from a fresh WP_Query.
How do you debug a meta query?
The fastest way to debug a meta query is to look at the SQL WordPress generated and run it yourself.
After running the query, print $query->request. This shows the full SQL, including the joins to postmeta and the exact values used.
The free Query Monitor plugin shows every database query on a page, which component ran it and how long it took. Our guide on how to use Query Monitor walks through its panels.
Turn on debugging while you test so PHP notices are not hidden. The steps are in how to enable debug mode in WordPress.
A good debugging order looks like this:
- Confirm the post type and status are correct.
- Print the stored meta for one post you expect to match.
- Remove all clauses but one and test again.
- Add the correct
typefor numbers and dates. - Print
$query->requestand run the SQL in a database tool. - Add clauses back one at a time.
How do you make meta queries faster?
Keep meta queries simple, limit the number of clauses and avoid LIKE on large sites, because each clause adds a join to the postmeta table.
The postmeta table can hold millions of rows on busy sites. Queries with many clauses or wildcard searches can become slow.
Cache the results of expensive queries with the Transients API or an object cache, so they do not run on every page load.
If you filter by the same field all the time, a custom taxonomy may suit the data better. Taxonomy queries are usually faster for grouping posts by fixed values such as colour or brand.
Frequently asked questions
Why does my meta query return all posts?
This usually means WordPress ignored the meta query because the array was not nested correctly, or the argument name was misspelled. Check that meta_query is an array of clause arrays and print the generated SQL to confirm the postmeta join is present.
Why does my meta query return no posts?
The key or value probably does not match what is stored, or the post type is wrong. Print the stored meta for a post you expect to match and compare it character by character with your query, including case and number format.
How do I compare numbers in a meta query?
Set the type to NUMERIC, or DECIMAL for values with decimals, and use operators such as greater than, less than or BETWEEN. Without a type, WordPress compares values as text, which gives wrong results for numbers of different lengths.
Can I query serialized custom field data?
Only roughly. A LIKE comparison can find a value inside a serialized string, but it may match the wrong items and is slow on large sites. Storing each value as a separate meta row is the more reliable approach.
Are meta queries bad for performance?
They can be on large sites, since each clause joins the postmeta table. Keep clauses few, avoid wildcard searches, cache results and consider a custom taxonomy for values you filter by often.
Sources
- WP_Meta_Query WordPress Developer Resources
- WP_Query WordPress Developer Resources
- Query Monitor WordPress.org Plugins
- Query posts by custom fields Advanced Custom Fields
- get_post_meta() WordPress Developer Resources
Is your product missing, or something out of date?
Tell us what to add or correct. We read every note and reply when we need a source. A message does not buy a ranking.
Thanks — we review requests within 3 business days.
Related guides
About the editors
Researched from the primary sources listed above and checked against our editorial rules before publishing.


