[wp-hackers] Query: meta_key !work right with meta_query

Robert Lusby nanogwp at gmail.com
Wed Jan 11 17:14:38 UTC 2012

Loading posts using a custom meta query in WP_Query. Then trying to use 
a second meta key to sort the data.

However including meta_key makes WP ignore the meta_query. Not including 
meta_key makes WP ignore the sort order.

How do you extract posts with one meta key/value pair, and then sort 
using a different meta key? Am I missing something obvious? This doesn't 
seem expected behaviour.

$args =

'meta_key' => 'secondkey',
'orderby' => 'meta_value title',
'order' => 'ASC',
'meta_query' => array(
     'relation' => 'OR',
         'key' => 'key1',
         'value' => 'value1',
         'compare' => '='
         'key' => 'key1',
         'value' => 'key1',
         'compare' => '='

$the_query = new WP_Query($args);


More information about the wp-hackers mailing list