[wp-meta] [Making WordPress.org] #2173: Error when WP_Query parses orderby array in function parse_order

Making WordPress.org noreply at wordpress.org
Fri Oct 21 21:41:57 UTC 2016


#2173: Error when WP_Query parses orderby array in function parse_order
---------------------+------------------------------------
 Reporter:  oloynet  |      Owner:
     Type:  defect   |     Status:  new
 Priority:  high     |  Milestone:
Component:  API      |   Keywords:  dev-feedback has-patch
---------------------+------------------------------------
 Example of request where I want to order by two custom fields in meta
 query
 start_date_order and is_sticky

 {{{
 $args = array(
     'no_found_rows'  => true ,
     'posts_per_page' => 4,
     'post_type'      => 'event',
     'post_status'    => 'publish',

     'meta_query' => array(
         'relation' => 'AND',
         array(
             'key'  => 'start_date_order',
             'type' => 'UNSIGNED',
         ),
         array(
             'key'  => 'is_sticky',
             'type' => 'UNSIGNED',
         ),
     ),
     'orderby' => array(
         'start_date_order' => 'DESC',
         'is_sticky'        => 'ASC',
     ),
 );
 }}}

 The '$primary_meta_query' var in method parse_order( $order ) is set
 forever with the first item of '$meta_clauses' array

 see line 2336 /wp-includes/query.php

 {{{#!php
 <?php
 $primary_meta_query = reset( $meta_clauses );
 }}}


 I fix the problem with the following PHP code

 {{{#!php
 <?php
 //$primary_meta_query = reset( $meta_clauses );

 $primary_meta_query = array();
 foreach( $meta_clauses as $meta_clause ) {

     if( $meta_clause['key'] == $orderby ) {
         $primary_meta_query = $meta_clause;
         break;
     }
 }

 }}}

 Now the good sql query is look like :


 {{{
 SELECT wp_posts.ID
 FROM wp_posts
 INNER JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id )
 INNER JOIN wp_postmeta AS mt1 ON ( wp_posts.ID = mt1.post_id )
 WHERE 1=1
 AND (
   wp_postmeta.meta_key = 'start_date_order'
   AND mt1.meta_key = 'is_sticky'
 )
 AND wp_posts.post_type = 'event'
 AND ((wp_posts.post_status = 'publish'))
 GROUP BY wp_posts.ID
 ORDER BY CAST(wp_postmeta.meta_value AS UNSIGNED) DESC,
 CAST(mt1.meta_value AS UNSIGNED) ASC
 LIMIT 0, 3
 }}}

--
Ticket URL: <https://meta.trac.wordpress.org/ticket/2173>
Making WordPress.org <https://meta.trac.wordpress.org/>
Making WordPress.org


More information about the wp-meta mailing list