[wp-trac] [WordPress Trac] #16814: WP_Query doesn't check orderby for Custom Post Types querying with meta_query

WordPress Trac wp-trac at lists.automattic.com
Thu Mar 10 00:21:52 UTC 2011


#16814: WP_Query doesn't check orderby for Custom Post Types querying with
meta_query
--------------------------+-----------------------------
 Reporter:  dalesaurus    |      Owner:
     Type:  defect (bug)  |     Status:  new
 Priority:  normal        |  Milestone:  Awaiting Review
Component:  Query         |    Version:  3.1
 Severity:  normal        |   Keywords:
--------------------------+-----------------------------
 With the new join style in 3.1 for Custom Post Types, an array must be set
 on WP_Query with '''meta_query'''.  In query.php line 2274 all cases of
 '''orderby''' are processed, specifically creating an array called
 ''$allowed_keys''.

 This check looks only for the deprecated '''meta_key'''.  This will cause
 any sorting on a Custom Post Type meta_value to fail.

 {{{
 // Fix proposal (until meta_key goes away)
 if ( !empty($q['meta_query']) || !empty($q['meta_key']) ) {
     $allowed_keys[] = $q['meta_key'];
     $allowed_keys[] = 'meta_value';
     $allowed_keys[] = 'meta_value_num';
 }

 }}}

 Temporary workaround for people with broken sorts is to set '''meta_key'''
 in addition to setting the '''meta_query''' variable.

 {{{
 // Example 1
 set_query_var( 'meta_query', array( array( 'key' => 'my_key_name',
 'compare' => '>','value' => '1','type' => 'NUMERIC' ) ) );
 set_query_var( 'orderby','meta_value' );
 set_query_var( 'meta_key', 'my_key_name' ); // need this


 // Example 2
 $wpq->set( 'meta_query', array(
           array(
                 'key' => 'my_key_name',
                 'compare' => '>',
                 'value' => '1',
                 'type' => 'NUMERIC'
           )
        )
     );
 $wpq->set( 'orderby','meta_value' );
 $wpq->set( 'meta_key', 'my_key_name' ); // need this
 }}}

-- 
Ticket URL: <http://core.trac.wordpress.org/ticket/16814>
WordPress Trac <http://core.trac.wordpress.org/>
WordPress blogging software


More information about the wp-trac mailing list