WordPress.org

Make WordPress Core

Opened 2 years ago

Closed 8 months ago

#19525 closed enhancement (wontfix)

Meta Query failing to build a proper SQL Query

Reported by: peshkohacka Owned by:
Milestone: Priority: normal
Severity: normal Version: 3.1
Component: Query Keywords: has-patch
Focuses: Cc:

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)

19525.diff (493 bytes) - added by scribu 2 years ago.

Download all attachments as: .zip

Change History (8)

comment:1 DrewAPicture2 years ago

  • Cc xoodrew@… added

comment:2 GaryJ2 years ago

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.

comment:3 peshkohacka2 years 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.

comment:4 scribu2 years ago

  • Description modified (diff)

comment:5 scribu2 years ago

  • 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);

comment:6 scribu2 years ago

  • 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.

scribu2 years ago

comment:7 wonderboymusic8 months ago

  • Keywords 2nd-opinion removed
  • Milestone Awaiting Review deleted
  • Resolution set to wontfix
  • Status changed from reopened to closed

That would be the only _doing_it_wrong in the whole file, and is confusing because there are key-only queries allowed in there. Gonna close, this ticket hasn't had any steam for a few years anyways

Note: See TracTickets for help on using tickets.