[wp-trac] [WordPress Trac] #19525: Meta Query failing to build a proper SQL Query

WordPress Trac wp-trac at lists.automattic.com
Tue Dec 13 03:16:09 UTC 2011


#19525: Meta Query failing to build a proper SQL Query
--------------------------+------------------------------
 Reporter:  peshkohacka   |       Owner:
     Type:  defect (bug)  |      Status:  new
 Priority:  normal        |   Milestone:  Awaiting Review
Component:  Query         |     Version:  3.3
 Severity:  normal        |  Resolution:
 Keywords:                |
--------------------------+------------------------------
Description changed by scribu:

Old description:

> 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:
>

> {{{
> object(WP_Query)#1337 (44) {
>   ["query_vars"]=>
>   array(58) {
>     ["category_name"]=>
>     string(3) "customcat"
>     ["post_status"]=>
>     string(7) "publish"
>     ["meta_key"]=>
>     string(13) "time"
>     ["meta_value"]=>
>     string(9) "135784684"
>     ["meta_type"]=>
>     string(7) "numeric"
>     ["meta_compare"]=>
>     string(11) "NOT BETWEEN"
>     ["error"]=>
>     string(0) ""
>     ["m"]=>
>     int(0)
>     ["p"]=>
>     int(0)
>     ["post_parent"]=>
>     string(0) ""
>     ["subpost"]=>
>     string(0) ""
>     ["subpost_id"]=>
>     string(0) ""
>     ["attachment"]=>
>     string(0) ""
>     ["attachment_id"]=>
>     int(0)
>     ["name"]=>
>     string(0) ""
>     ["static"]=>
>     string(0) ""
>     ["pagename"]=>
>     string(0) ""
>     ["page_id"]=>
>     int(0)
>     ["second"]=>
>     string(0) ""
>     ["minute"]=>
>     string(0) ""
>     ["hour"]=>
>     string(0) ""
>     ["day"]=>
>     int(0)
>     ["monthnum"]=>
>     int(0)
>     ["year"]=>
>     int(0)
>     ["w"]=>
>     int(0)
>     ["tag"]=>
>     string(0) ""
>     ["cat"]=>
>     string(1) "7"
>     ["tag_id"]=>
>     string(0) ""
>     ["author_name"]=>
>     string(0) ""
>     ["feed"]=>
>     string(0) ""
>     ["tb"]=>
>     string(0) ""
>     ["paged"]=>
>     int(0)
>     ["comments_popup"]=>
>     string(0) ""
>     ["preview"]=>
>     string(0) ""
>     ["s"]=>
>     string(0) ""
>     ["sentence"]=>
>     string(0) ""
>     ["fields"]=>
>     string(0) ""
>     ["category__in"]=>
>     array(0) {
>     }
>     ["category__not_in"]=>
>     array(0) {
>     }
>     ["category__and"]=>
>     array(0) {
>     }
>     ["post__in"]=>
>     array(0) {
>     }
>     ["post__not_in"]=>
>     array(0) {
>     }
>     ["tag__in"]=>
>     array(0) {
>     }
>     ["tag__not_in"]=>
>     array(0) {
>     }
>     ["tag__and"]=>
>     array(0) {
>     }
>     ["tag_slug__in"]=>
>     array(0) {
>     }
>     ["tag_slug__and"]=>
>     array(0) {
>     }
>     ["ignore_sticky_posts"]=>
>     bool(false)
>     ["suppress_filters"]=>
>     bool(false)
>     ["cache_results"]=>
>     bool(true)
>     ["update_post_term_cache"]=>
>     bool(true)
>     ["update_post_meta_cache"]=>
>     bool(true)
>     ["post_type"]=>
>     string(0) ""
>     ["posts_per_page"]=>
>     int(10)
>     ["nopaging"]=>
>     bool(false)
>     ["comments_per_page"]=>
>     string(2) "50"
>     ["no_found_rows"]=>
>     bool(false)
>     ["order"]=>
>     string(4) "DESC"
>   }
>   ["tax_query"]=>
>   object(WP_Tax_Query)#1337 (2) {
>     ["queries"]=>
>     array(1) {
>       [0]=>
>       array(5) {
>         ["taxonomy"]=>
>         string(8) "category"
>         ["terms"]=>
>         array(1) {
>           [0]=>
>           string(3) "customcat"
>         }
>         ["include_children"]=>
>         bool(true)
>         ["field"]=>
>         string(4) "slug"
>         ["operator"]=>
>         string(2) "IN"
>       }
>     }
>     ["relation"]=>
>     string(3) "AND"
>   }
>   ["meta_query"]=>
>   object(WP_Meta_Query)#1337 (2) {
>     ["queries"]=>
>     array(1) {
>       [0]=>
>       array(4) {
>         ["key"]=>
>         string(13) "time"
>         ["compare"]=>
>         string(11) "NOT BETWEEN"
>         ["type"]=>
>         string(7) "numeric"
>         ["value"]=>
>         string(9) "135784684"
>       }
>     }
>     ["relation"]=>
>     string(3) "AND"
>   }
>   ["post_count"]=>
>   int(0)
>   ["current_post"]=>
>   int(-1)
>   ["in_the_loop"]=>
>   bool(false)
>   ["comment_count"]=>
>   int(0)
>   ["current_comment"]=>
>   int(-1)
>   ["found_posts"]=>
>   string(1) "1"
>   ["max_num_pages"]=>
>   float(1)
>   ["max_num_comment_pages"]=>
>   int(0)
>   ["is_single"]=>
>   bool(false)
>   ["is_preview"]=>
>   bool(false)
>   ["is_page"]=>
>   bool(false)
>   ["is_archive"]=>
>   bool(true)
>   ["is_date"]=>
>   bool(false)
>   ["is_year"]=>
>   bool(false)
>   ["is_month"]=>
>   bool(false)
>   ["is_day"]=>
>   bool(false)
>   ["is_time"]=>
>   bool(false)
>   ["is_author"]=>
>   bool(false)
>   ["is_category"]=>
>   bool(true)
>   ["is_tag"]=>
>   bool(false)
>   ["is_tax"]=>
>   bool(false)
>   ["is_search"]=>
>   bool(false)
>   ["is_feed"]=>
>   bool(false)
>   ["is_comment_feed"]=>
>   bool(false)
>   ["is_trackback"]=>
>   bool(false)
>   ["is_home"]=>
>   bool(false)
>   ["is_404"]=>
>   bool(false)
>   ["is_comments_popup"]=>
>   bool(false)
>   ["is_paged"]=>
>   bool(false)
>   ["is_admin"]=>
>   bool(false)
>   ["is_attachment"]=>
>   bool(false)
>   ["is_singular"]=>
>   bool(false)
>   ["is_robots"]=>
>   bool(false)
>   ["is_posts_page"]=>
>   bool(false)
>   ["is_post_type_archive"]=>
>   bool(false)
>   ["query_vars_hash"]=>
>   string(32) "5481e968160b92320b359a1818596c83"
>   ["query_vars_changed"]=>
>   bool(false)
>   ["thumbnails_cached"]=>
>   bool(false)
>   ["query"]=>
>   array(6) {
>     ["category_name"]=>
>     string(3) "customcat"
>     ["post_status"]=>
>     string(7) "publish"
>     ["meta_key"]=>
>     string(13) "time"
>     ["meta_value"]=>
>     string(9) "135784684"
>     ["meta_type"]=>
>     string(7) "numeric"
>     ["meta_compare"]=>
>     string(11) "NOT BETWEEN"
>   }
>   ["request"]=>
>   string(469) " 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"
>   ["posts"]=>
>   array(0) {
>   }
> }
> }}}
>

> 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.
>
> If it matters (all the vars are above, but still) 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);
> }}}

New description:

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

--

-- 
Ticket URL: <http://core.trac.wordpress.org/ticket/19525#comment:4>
WordPress Trac <http://core.trac.wordpress.org/>
WordPress blogging software


More information about the wp-trac mailing list