[wp-trac] [WordPress Trac] #31045: Ordering a Query Using Multiple Meta Keys

WordPress Trac noreply at wordpress.org
Sun Jan 18 13:41:58 UTC 2015


#31045: Ordering a Query Using Multiple Meta Keys
-----------------------------+-----------------------------
 Reporter:  Funkatronic      |      Owner:
     Type:  feature request  |     Status:  new
 Priority:  normal           |  Milestone:  Awaiting Review
Component:  Database         |    Version:  trunk
 Severity:  normal           |   Keywords:
  Focuses:  performance      |
-----------------------------+-----------------------------
 Currently, WordPress doesn't support sorting a query by multiple meta
 keys; we can only sort by one, even though we can now query posts by
 multiple meta entries.  My idea is that for meta queries, we allow the
 coder querying to assign the table aliases in the query arrays.  Either by
 adding a new argument:


 {{{
 $args = array(
         'post_type'  => 'product',
         'meta_query' => array(
                 array(
                         'key'     => 'color',
                         'value'   => 'blue',
                         'compare' => 'NOT LIKE',
                         'alias'   => 'color'
                 ),
         ),
         'orderby' => 'meta-color'  //prefix to prevent any collisiona
 );

 $query = new WP_Query( $args );
 }}}

 Or making array indexes the alias:

 {{{
 $args = array(
         'post_type'  => 'product',
         'meta_query' => array(
                 'color' => array(
                         'key'     => 'color',
                         'value'   => 'blue',
                         'compare' => 'NOT LIKE',
                 ),
         ),
         'orderby' => 'meta-color'  //prefix to prevent any collisiona
 );

 $query = new WP_Query( $args );
 }}}

 Or just use the meta key as the alias:

 {{{
 $args = array(
         'post_type'  => 'product',
         'meta_query' => array(
                 array(
                         'key'     => 'color',
                         'value'   => 'blue',
                         'compare' => 'NOT LIKE',
                 ),
         ),
         'orderby' => 'meta-color'  //prefix to prevent any collisiona
 );

 $query = new WP_Query( $args );
 }}}

 In either case, the query would denote the alias used for the table in the
 mySQL query, then you can specify in the orderby clause which ones you
 want to sort by, allowing for multiple if the need be.

--
Ticket URL: <https://core.trac.wordpress.org/ticket/31045>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list