Opened 14 years ago
Closed 10 years ago
#16814 closed defect (bug) (fixed)
WP_Query doesn't check orderby for meta_query
Reported by: | dalesaurus | Owned by: | kovshenin |
---|---|---|---|
Milestone: | 4.1 | Priority: | normal |
Severity: | normal | Version: | 3.1 |
Component: | Query | Keywords: | has-patch commit |
Focuses: | Cc: |
Description
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
Attachments (3)
Change History (15)
#4
@
14 years ago
- Milestone Awaiting Review deleted
- Resolution set to duplicate
- Status changed from new to closed
- Summary changed from WP_Query doesn't check orderby for Custom Post Types querying with meta_query to WP_Query doesn't check orderby for meta_query
First of all, this has nothing to do with custom post types. It affects regular posts as well.
I'm closing this ticket as a duplicate of #15031, since it's basically the same problem.
Feel free to join the discussion there.
#6
@
10 years ago
- Keywords has-patch added
- Milestone set to Awaiting Review
- Resolution duplicate deleted
- Status changed from closed to reopened
I'd like to reopen this, because r25255 has added even more inconsistencies between a meta_query and a combination of meta_key, meta_value, etc. When providing a meta_type key, orderby=meta_value will cast to that type, but a type key in a meta_query won't.
In fact, a meta_query won't even add meta strings to $allowed_keys as stated by the reporter. This is a bit ironic, because a combination of meta_key, meta_value, etc. does resolve into a meta_query.
16814.diff is a proposed fix + tests.
#7
@
10 years ago
- Milestone changed from Awaiting Review to 4.1
Confirmed. Good catch.
Your fix looks correct to me. To spell it out a bit more for posterity: when passing meta_key
, etc to WP_Query
, they are parsed into a WP_Meta_Query
object: https://core.trac.wordpress.org/browser/tags/4.0/src/wp-includes/query.php#L2377. Any combination of meta_key
, meta_value
etc that were passed to WP_Query
always gets set as the 0th member of the meta_query->queries
array. So looking at the 0th index, as in your patch, will find the right orderby regardless of whether you're using meta_key
etc or a proper meta_query
param.
16814.2.patch adds another assertion to your test, to show how the meta_key
gets parsed together with, and takes precedence over, the meta_query
arguments.
#8
@
10 years ago
I was able to recreate the queries that prompted this ticket back in 2011. The 16814.2.patch? does resolve the orderby issue patched against trunk r29617.
Thanks to you both for digging this up!
#9
follow-up:
↓ 10
@
10 years ago
- Keywords commit added
dalesaurus - Thanks for the confirmation!
kovshenin - You want to do the honors? :)
#10
in reply to:
↑ 9
@
10 years ago
Replying to boonebgorges: Your patch introduced a $primary_meta_query
variable but it's not used. Was the intent to use that instead of $meta_query
and avoid confusion between $meta_query
, $this->meta_query
and $this->get('meta_query')
?
In 16814.3.diff I also changed $meta_key
to $primary_meta_key
and $meta_type
to $sql_type
.
#11
@
10 years ago
Ah right, I'd forgotten that I did that. Yes, I'd originally changed the variable name because it took me a second to grok what you were using it for; I changed it back, but then forgot to remove the original $primary_meta_query
declaration. Sorry about that.
I like the new variable names better, personally, but I'm happy to defer to your judgment.
in your example in the description: $wpq ?