Make WordPress Core

Opened 9 years ago

Closed 9 years ago

#31265 closed task (blessed) (fixed)

Metadata 'orderby' parity across query classes

Reported by: boonebgorges's profile boonebgorges Owned by:
Milestone: 4.2 Priority: normal
Severity: normal Version:
Component: Query Keywords: needs-patch
Focuses: Cc:

Description (last modified by boonebgorges)

WP_Query, WP_User_Query, and WP_Comment_Query all have some support for ordering results by metadata (postmeta, usermeta, or commentmeta). But the support is inconsistent. WP_Query should be the reference, and we should sweep through WP_User_Query and WP_Comment_Query to ensure that they're up to snuff. Things to check:

  • --'orderby' should accept space-separated values ('orderby' => 'comment_user_ID comment_ID') as well as the array syntax introduced to WP_Query in #17065. See #30478.-- [done]
  • meta_value, meta_key, meta_type, and meta_compare should all be supported, and should be translated to a meta_query clause in the same way (using WP_Meta_Query::parse_query_vars(), I suppose)
  • orderby=meta_value, orderby=meta_value_num, and orderby=somekey when meta_key=somekey should be supported. See #27887.
  • orderby=clausename, where 'clausename' is the array-key identifier of a meta_query clause. See #31045 for how this was done in WP_Query.

Some of this is done, some is definitely not, some I'm too lazy to check at the moment. All of it needs unit tests.

Change History (11)

#1 @SergeyBiryukov
9 years ago

  • Description modified (diff)

#2 @boonebgorges
9 years ago

In 31467:

Improve 'orderby' syntax for WP_Comment_Query.

Since [29027], WP_Query has supported an array of values for the $orderby
parameter, with field names as array keys and ASC/DESC as the array values.
This changeset introduces the same syntax to WP_Comment_Query.

We leverage the new support for multiple ORDER BY clauses to fix a bug that
causes comments to be queried in an indeterminate order when sorting by the
default comment_date_gmt and comments share the same value for
comment_date_gmt. By always including a comment_ID subclause at the end of
the ORDER BY statement, we ensure that comments always have a unique fallback
for sorting.

This changeset also includes improvements paralleling those introduced to
WP_Query in [31312] and [31340], which allow $orderby to accept array keys
from specific $meta_query clauses. This change lets devs sort by multiple
clauses of an associated meta query. See #31045.

Fixes #30478. See #31265.

#3 @boonebgorges
9 years ago

In 31662:

Tests for some existing 'orderby' functionality in WP_*_Query classes.

  • In WP_Query and WP_Comment_Query, ensure that 'orderby' can parse multiple values for 'orderby' when passed as a space-separated string.
  • In WP_User_Query, ensure that "shorthand" orderbys (like 'login' and 'name') are converted to their full versions (like 'user_login' and 'display_name').

See #31265.

#4 @boonebgorges
9 years ago

In 31663:

Improve 'orderby' syntax for WP_User_Query.

This changeset ports a number of 'orderby' features from WP_Query and

  • Allow multiple 'orderby' values to be passed as a space-separated list.
  • Allow multiple 'orderby' values to be passed as a flat array.
  • Allow multi-dimensional 'orderby', with orderby fields as array keys and ASC/DESC as the corresponding values.

See #31265.

#5 @boonebgorges
9 years ago

In 31664:

Fix documentation whitespace after [31663].

See #31265.

#6 @boonebgorges
9 years ago

  • Description modified (diff)

#7 @boonebgorges
9 years ago

In 31665:

In WP_User_Query, $meta_query should be a class property rather than a local variable.

This provides better parity with other query classes, and makes it possible to
write more direct unit tests.

See #31265.

#8 @boonebgorges
9 years ago

In 31666:

Add tests for the conversion of 'meta_*' query vars to $meta_query objects in all query classes.

See #31265.

#9 @boonebgorges
9 years ago

In 31668:

User query meta_vars test must account for the 'blog_id' clause added in multisite.

See #31265.

#10 @boonebgorges
9 years ago

In 31669:

Improved 'orderby' meta syntax in WP_User_Query.

Recent commits have added the ability to order query results by specific
clauses of the 'meta_query' parameter (comments [31467], posts [31312] and
[31340]). The current changeset ports the same functionality to WP_User_Query.

Also introduced is the ability to pass the value of $meta_key to 'orderby'.

The internals of WP_User_Query::prepare_users() had to be reordered
somewhat to support these changes, primarily to ensure that the meta_query
object generates its SQL clauses before the 'orderby' parameter is parsed.

See #31265.

#11 @boonebgorges
9 years ago

  • Milestone changed from Future Release to 4.2
  • Resolution set to fixed
  • Status changed from new to closed

I believe this is now finished.

Note: See TracTickets for help on using tickets.