WordPress.org

Make WordPress Core

Opened 4 years ago

Last modified 6 weeks ago

#15031 closed enhancement

order via meta_query — at Version 6

Reported by: aaroncampbell Owned by: scribu
Milestone: Priority: normal
Severity: normal Version: 3.1
Component: Query Keywords: dev-feedback 2nd-opinion
Focuses: Cc:

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 (6)

comment:1 aaroncampbell4 years ago

  • Component changed from General to Query

comment:2 aaroncampbell4 years ago

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.

comment:3 aaroncampbell4 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.

comment:4 scribu4 years ago

  • 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.

comment:5 scribu4 years ago

(In [15721]) Put the simple meta query first, so that orderby=meta_value works even when using 'meta_query'. See #15031

comment:6 scribu3 years ago

  • Description modified (diff)
  • Summary changed from WP_Query can no longer sort by meta value to order via meta_query
Note: See TracTickets for help on using tickets.