-
Notifications
You must be signed in to change notification settings - Fork 2.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add multiple orderby support to REST Posts Controller #1186
Conversation
9e72d15
to
caa7486
Compare
@@ -1012,6 +1049,8 @@ protected function prepare_items_query( $prepared_args = array(), $request = nul | |||
* | |||
* @param string $value The query_var value. | |||
*/ | |||
// TODO: Do not merge without auditing filters | |||
// BREAKING CHANGE! Other filters too? orderby was a string but is now a PHP array (either a REST array or a REST object). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Highlighting that we still need to 🤔 audit existing filters
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For the rest_query_var-orderby
filter I do think it's clearest to simply pass through value, which might be a string or array.
If we'd really like to preserve backwards compatibility, we could do something like pass through the first argument as a string (maybe the first value or some comma or space delimited string), and have the second argument be the original value. I feel like it'd avoid potential errors on upgrade, but would be confusing for future usage.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Taking another look, it looks like the default implementation returns $value so passing through a string as a first arg won't work.
src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php
Outdated
Show resolved
Hide resolved
93da3b9
to
fe70bae
Compare
src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php
Outdated
Show resolved
Hide resolved
035addd
to
cbe69f3
Compare
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 |
8c26557
to
04b0929
Compare
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 FiltersThere 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 |
…e posts controller
c51989f
to
1ddfce5
Compare
I think test failures here mean that fixtures need to be regenerated for |
Running the rest api phpunit test suite should regenerate those files. |
Fixes https://core.trac.wordpress.org/ticket/39037 This PR adds multiple orderby support to the REST posts controller.
As an experiment, I also added a secondary ID sort to stabilize the order. This would help address pagination issues, without forcing the caller to set a second sort. Another way of fixing that would be doing so in WP_Query versus the REST posts controller, but that change would have a much larger surface area.Reading through other issues, I think folks were preferring this be done at the WP_Query level.Related: https://core.trac.wordpress.org/ticket/46294 https://core.trac.wordpress.org/ticket/44349
Problem
We can currently only sort by a single dimension using the REST API. In some cases, we need a second sort to help provide a stable order. For example imagine if we sort by
menu_order
, if other items don't have this value set, what order should they be sorted by? id? title?What happens currently is that internal implementation details affect this ordering, sometimes in surprising ways. One example is simply changing a limit from 499 to 500 when using menu_order. This is because at a limit of <500, WP_Query internally splits a post query into two queries: issue, changeset.
Proposed changes here allow us to sort with multiple orderby values, to provide more consistent ordering in results.
TODO
Testing Instructions
If folks don't have an rest client handy, we can open the console when visiting the post editor: