[wp-hackers] meta_query orderby bug
Hameedullah Khan
h at hameedullah.com
Fri Jun 17 10:25:01 UTC 2011
Hello Everyone,
So this is my first bug that I was about to report. Then I read that I
should discuss this on #IRC or on mailing list. This is the first time
I am reporting a bug so please bare with me. :) Let me just describe
you the issue.
If we specify meta_key and an orderby query var. The way wordpress
handles them is that it adds them to the meta_query query var which is
an array of array. Wordpress adds new meta_query array to existing
meta_query arrays but the new array as inserted as the first element.
For example if I specified following:
$meta_key = 'priority';
$orderby = 'meta_value_num';
$meta_query = array(
array(
'key' => 'weight',
'value' => 2,
'compare' => '>=',
'type' => 'NUMERIC'
)
);
Then after Wordpress completes parsing the meta_query in function
_parse_meta_query at line #458 in file: wp-includes/meta.php The
resulting meta_query looks like following:
$meta_key = 'priority';
$orderby = 'meta_value_num';
$meta_query = array(
array(
'key' => 'priority'
),
array(
'key' => 'weight',
'value' => 2,
'compare' => '>=',
'type' => 'NUMERIC'
)
);
This all works fine if I have specified my meta_query and orderby
query vars before the pre_get_posts filter is run. But if I add
orderyby and meta_key in pre_get_posts filter then when WordPress
parses the meta_query again, instead of inserting the new array as the
first element it appends the new array to existing meta_query. And the
resulting meta_query looks like following:
$meta_query = array(
array(
'key' => 'weight',
'value' => 2,
'compare' => '>=',
'type' => 'NUMERIC'
),
array(
'key' => 'priority'
)
);
And when the meta_sql is generated the orderby is applied to the first
meta key value specified in meta_query. The problematic line is #2470
in wp-includes/query.php which is:
$q['meta_query'] = array_merge( $_meta_query, $q['meta_query'] );
But it should be:
$q['meta_query'] = array_merge( $q['meta_query'], $_meta_query );
So, is it really a bug? Should I report it on trac and be proud of it?
Or this is intended behavior?
--
Regards
Hameedullah Khan
w: http://hameedullah.com
t: @hameedullah
More information about the wp-hackers
mailing list