Make WordPress Core

Opened 5 years ago

Last modified 5 months ago

#39037 new enhancement

Should REST API support multiple orderby values?

Reported by: ChopinBach Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 4.6.1
Component: REST API Keywords: has-patch has-unit-tests
Focuses: Cc:


WP_Query features the ability to query by multiple orderby parameters as well as independent orderby parameters (https://core.trac.wordpress.org/ticket/17065). The API does not have this ability yet. I looked through GitHub issues to find any discussion regarding this and could not find any. Should the REST API also support multiple orderby parameters or is that deemed to be plugin territory?

Change History (8)

#1 @jnylen0
5 years ago

What would it take to support this as a feature of the API?

Is it possible to make this work via existing filters? (This would also be a useful exercise to document adding orderby parameters via filters and determining whether there is anything left to be fixed there - see ticket:38693#comment:22 for a simpler case which we haven't tried yet)

#2 follow-up: @jnylen0
5 years ago

Another question: do other object types (WP_Comment_Query etc) support this?

#3 in reply to: ↑ 2 @ChopinBach
5 years ago

Replying to jnylen0:

Is it possible to make this work via existing filters?

Implementing multiple orderby values is not too difficult to implement. Doing the independent variation where some fields are ASC and some DESC would be considerably harder to put into the API. I am pretty sure you can do both by using multiple filters in the API.

Another question: do other object types (WP_Comment_Query etc) support this?

From a quick look, it appears that only the standard WP_Query object supports multiple and independent orderby parameters.

#4 @ChopinBach
5 years ago

WP_User_Query does support multiple orderby parameters.

#5 @David_sa
15 months ago

Any progress on this?

This ticket was mentioned in PR #1186 on WordPress/wordpress-develop by gwwar.

6 months ago

  • Keywords has-patch has-unit-tests added

Fixes https://core.trac.wordpress.org/ticket/39037 to add multiple orderby values to the REST posts controller.

### Problem

When sorting by a single dimension such as menu_order many items may be equal to one another. Currently we don't provide or intend to provide a stable sort for those equal items. To avoid this we commonly provide a second dimension to sort by like title, which currently isn't possible in the API.

Internal implementation details also affect ordering, sometimes in surprising ways. One example is simply changing a limit from 499 to 500. At a limit of <500, WP_Query internally splits a post query into two queries: issue, changeset.

API usage in the block editor limits per_page to 100 per request, so in many cases the order fetched by the client and one created in a block render_callback will not match.


### TODO

  • [ ] Audit affected filters
  • [ ] Smoke test other affected rest endpoints

### Testing Instructions

  • Verify that tests pass and make sense
  • Manually test with a few requests in the following format:

If folks don't have an rest client handy, we can open the console when visiting the post editor:

wp.apiFetch( { path: '/wp/v2/pages?per_page=100&orderby[menu_order]=asc&orderby[title]=desc' } ).then( ( data ) => { 
    console.log( data );
} );

#7 @prbot
5 months ago

gwwar commented on PR #1186:

Reading through https://core.trac.wordpress.org/ticket/47642#comment:13 it looks like folks do prefer this at the WP_Query level. I’ll back out the other commits from this PR

#8 @prbot
5 months ago

gwwar commented on PR #1186:

I circled back to this one and it's ready for another look 👀 In terms of feedback I have two main areas that would be helpful:

#### Affected Filters

There are two filters that now have breaking changes. What do folks think might be the best way to resolve this?

#### Any missing test coverage?

Are there any areas that I should add tests / verify changes for?

cc @TimothyBJacobs @mdawaffe or whomever may be interested

Note: See TracTickets for help on using tickets.