Opened 3 years ago
Last modified 13 days ago
#15031 assigned enhancement
order via meta_query
| Reported by: |
|
Owned by: | |
|---|---|---|---|
| Priority: | normal | Milestone: | Future Release |
| Component: | Query | Version: | 3.1 |
| Severity: | normal | Keywords: | dev-feedback 2nd-opinion |
| Cc: | gary@…, maor@…, ben@…, knut@…, vanjwilson |
Description (last modified by scribu)
#14645 added the ability to query based on multiple meta keys, but you can't use it to order posts.
Change History (21)
comment:1
aaroncampbell — 3 years ago
- Component changed from General to Query
comment:2
aaroncampbell — 3 years ago
comment:3
aaroncampbell — 3 years ago
- Keywords dev-feedback added
Adding the capability to _wp_meta_sql() was pretty simple. However, where it's currently called from any order by options would be appended to whatever was already calculated, which means that meta data ordering would always have to be secondary. You could NOT order by 'some_meta_value, date' only by 'date,some_meta_value' or just 'some_meta_value'. If we move it to being processed BEFORE the existing order_by is processed, then the reverse is true.
It seems like the correct solution would actually be to have some kind of place holder to be allowed, and roll back to appending if no place holders were used?
Sounds like a pain, so I'd like some input.
- Milestone changed from 3.1 to Future Release
- Type changed from defect (bug) to enhancement
It turns out this combination still works:
query_posts( array( 'meta_key' => 'foo', 'orderby' => 'meta_value' ) );
So it's not a regression.
Moving to Future until we can figure out a better solution.
- Description modified (diff)
- Summary changed from WP_Query can no longer sort by meta value to order via meta_query
I was thinking of something like this:
'meta_query' => array(
array(
'meta_key' => 'foo',
'order' => 'ASC',
'order_priority' => 2
),
array(
'meta_key' => 'bar',
'order' => 'DESC',
'order_priority' => 1
),
)
which would result in this SQL:
ORDER BY wp_postmeta_alias2.meta_value DESC, wp_postmeta_alias1.meta_value ASC
comment:8
in reply to:
↑ 7
aaroncampbell — 2 years ago
- Keywords 2nd-opinion added
Replying to scribu:
I like the idea, but I have a couple questions:
- Do we need order_priority? Can we just ask that people add the meta queries in the order they want them handled? To create the same sql as you have above you'd just do this:
'meta_query' => array( array( 'meta_key' => 'bar', 'order' => 'DESC', ), array( 'meta_key' => 'foo', 'order' => 'ASC', ), ) - Also, How do we plan to integrate these order clauses with the existing order stuff? For example, would it be possible to sort by "date, meta_value" or "meta_value, date"? Maybe the order_priority could be useful here, assuming that other order clauses are priority x, and anything with a priority < x comes before and anything with priority > x comes after? I know we don't have to offer every single option, but I think we need to decide what would be useful and make sure we account for that.
I'd also like to bounce this off a couple more people and get additional feedback.
- I guess implicit priority could work; the traditional 'order' => 'meta_value' would always be first.
- No clue.
comment:10
pento — 2 years ago
- Cc gary@… added
For 2, how about allowing orderby to take a string, or an array? It maintains backwards compatibility, but would also allow:
'orderby' => array(
array(
'meta_key' => 'bar',
'order' => 'DESC',
),
array(
'key' => 'date',
'order' => 'DESC',
),
array(
'meta_key' => 'foo',
'order' => 'ASC',
),
)
comment:11
scribu — 2 years ago
Related: #17065
comment:12
scribu — 2 years ago
I guess that in the end, allowing an array of values to 'orderby' makes more sense than an 'order' parameter in 'meta_query':
MySQL allows you to order items by a value, even if it's null. The fact that we required meta_key to be set, in order to set 'orderby' => 'meta_value' is just a limitation on our system.
So, I'm thinking of closing this as wontfix and focus on #17065.
comment:13
pento — 2 years ago
If #17065 will cover meta keys as well, that sounds reasonable to me.
comment:14
scribu — 2 years ago
- Milestone Future Release deleted
- Resolution set to wontfix
- Status changed from new to closed
Closing. See #17065
comment:15
maorb — 2 years ago
- Cc maor@… added
comment:16
scribu — 9 months ago
Marked #21617 as dup.
comment:17
scribu — 9 months ago
- Milestone set to Future Release
- Resolution wontfix deleted
- Status changed from closed to reopened
Going to re-open this, as 'wontfix' isn't the appropriate resolution.
If #17065 is implemented and covers this, we can close it as a dup.
comment:18
husobj — 8 months ago
- Cc ben@… added
comment:19
knutsp — 6 months ago
- Cc knut@… added
comment:20
scribu — 6 months ago
- Owner scribu deleted
- Status changed from reopened to assigned
comment:21
vanjwilson — 13 days ago
- Cc vanjwilson added

The plan is to add another array element to each meta_query array called 'order'. You would be able to pass ASC or DESC and empty or not set would mean "don't order based on this". I'm also incorporating the +0 fix into this if you pass 'numeric'=>true so that the order is as expected for numeric types.