Opened 17 months ago
Last modified 17 months ago
#19525 reopened enhancement
Meta Query failing to build a proper SQL Query
| Reported by: |
|
Owned by: | |
|---|---|---|---|
| Priority: | normal | Milestone: | Awaiting Review |
| Component: | Query | Version: | 3.1 |
| Severity: | normal | Keywords: | 2nd-opinion has-patch |
| Cc: | xoodrew@… |
Description (last modified by scribu)
I was tryin' to get meta working and i was wondering why no results were returned until i found that the query is actually invalid:
SELECT SQL_CALC_FOUND_ROWS * FROM posts INNER JOIN term_relationships ON (ID = term_relationships.object_id) INNER JOIN postmeta ON (ID = postmeta.post_id) WHERE 1=1 AND ( term_relationships.term_taxonomy_id IN (1) ) AND post_type = 'post' AND (post_status = 'publish') AND ( (postmeta.meta_key = 'time' AND ) GROUP BY ID ORDER BY post_date DESC LIMIT 0, 10
As you can see the meta query only inputs the meta_key and not the meta value/compare operator and as such the query ends up invalid.
This is how the query is getting called:
$args = array(
'category_name' => 'customcat',
'post_status' => 'publish',
'meta_key' => 'time',
'meta_value' => '135784684',
'meta_type' => 'numeric',
'meta_compare' => 'NOT BETWEEN'
);
$posts = new WP_Query($args);
Attachments (1)
Change History (7)
comment:1
DrewAPicture — 17 months ago
- Cc xoodrew@… added
comment:3
peshkohacka — 17 months ago
- Severity changed from major to normal
i tried both as single meta_* array keys, or a single meta_query array. Both doesn't work. Although if i tried another logic operator like < it works.
- Milestone Awaiting Review deleted
- Resolution set to invalid
- Status changed from new to closed
NOT BETWEEN requires an array for the value:
$args = array(
'category_name' => 'customcat',
'post_status' => 'publish',
'meta_key' => 'time',
'meta_value' => array( 1, 1000 ),
'meta_type' => 'numeric',
'meta_compare' => 'NOT BETWEEN'
);
$posts = new WP_Query($args);
- Keywords 2nd-opinion has-patch added
- Milestone set to Awaiting Review
- Resolution invalid deleted
- Status changed from closed to reopened
- Type changed from defect (bug) to enhancement
- Version changed from 3.3 to 3.1
Actually, I guess we could issue a warning, as it's pretty hard to figure out what's going on.
Note: See
TracTickets for help on using
tickets.

According to the documentation, the string "NOT BETWEEN" is not one of the supported comparison operators.
Try rewriting as a meta_query, which has been available since 3.1 and which does allow "NOT BETWEEN" as the comparison string.