[wp-trac] [WordPress Trac] #47286: WP_Query final SQL replaces int numbers to string in meta_query

WordPress Trac noreply at wordpress.org
Mon May 20 11:48:52 UTC 2019


#47286: WP_Query final SQL replaces int numbers to string in meta_query
--------------------------+------------------------------
 Reporter:  eddr          |       Owner:  (none)
     Type:  defect (bug)  |      Status:  new
 Priority:  normal        |   Milestone:  Awaiting Review
Component:  Query         |     Version:  5.2
 Severity:  normal        |  Resolution:
 Keywords:                |     Focuses:
--------------------------+------------------------------
Description changed by SergeyBiryukov:

Old description:

> WP_Query meta_query numeric value results in comparison to a string value
>
> 1.
> {{{#!php
> <?php
> function intercept_query_clauses( $pieces )
> {
>         var_dump( $pieces );
>         return $pieces;
> }
> add_filter( 'posts_where', 'intercept_query_clauses', 1, 1 );
> $query_args = [
>         'meta_query' => [[
>                 'key'     => 'key',
>                 'value'   => 10,
>                 'compare' => "="
>
>         ]]
>         , 'fields' => 'ids'
>
> ];
> $query = new WP_Query( $query_args );
> }}}
>

> result:
>
> **AND (
>   ( wp_postmeta.meta_key = 'key' AND ''wp_postmeta.meta_value = '10''' )
> ) AND wp_posts.post_type = 'post' AND (wp_posts.post_status = 'publish'
> OR wp_posts.post_status = 'private')**
>
> 2. using 'type' => numeric results in
> **AND (
>   ( wp_postmeta.meta_key = 'key' AND ''CAST(wp_postmeta.meta_value AS
> SIGNED)'' = '10' )
> ) AND wp_posts.post_type = 'post' AND (wp_posts.post_status = 'publish'
> OR wp_posts.post_status = 'private')"**
>
> which doesn't solve the problem + why casting the field and not the
> value?
>
> Am I missing something big here?
>
> Thanks

New description:

 WP_Query meta_query numeric value results in comparison to a string value

 1.
 {{{#!php
 <?php
 function intercept_query_clauses( $pieces )
 {
         var_dump( $pieces );
         return $pieces;
 }
 add_filter( 'posts_where', 'intercept_query_clauses', 1, 1 );
 $query_args = [
         'meta_query' => [[
                 'key'     => 'key',
                 'value'   => 10,
                 'compare' => "="

         ]]
         , 'fields' => 'ids'

 ];
 $query = new WP_Query( $query_args );
 }}}

 result:
 {{{
 AND (
   ( wp_postmeta.meta_key = 'key' AND wp_postmeta.meta_value = '10' )
 ) AND wp_posts.post_type = 'post' AND (wp_posts.post_status = 'publish' OR
 wp_posts.post_status = 'private')
 }}}

 2. using 'type' => numeric results in

 {{{
 AND (
   ( wp_postmeta.meta_key = 'key' AND CAST(wp_postmeta.meta_value AS
 SIGNED) = '10' )
 ) AND wp_posts.post_type = 'post' AND (wp_posts.post_status = 'publish' OR
 wp_posts.post_status = 'private')"
 }}}

 which doesn't solve the problem + why casting the field and not the value?

 Am I missing something big here?

 Thanks

--

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/47286#comment:1>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list