[wp-trac] [WordPress Trac] #39055: REST API: order is ignored when orderby is set to include

WordPress Trac noreply at wordpress.org
Thu Feb 2 20:02:01 UTC 2017


#39055: REST API: order is ignored when orderby is set to include
-------------------------------------------------+-------------------------
 Reporter:  joshlevinson                         |       Owner:  jnylen0
     Type:  enhancement                          |      Status:  accepted
 Priority:  normal                               |   Milestone:  4.8
Component:  Query                                |     Version:
 Severity:  normal                               |  Resolution:
 Keywords:  has-patch has-unit-tests needs-      |     Focuses:
  refresh                                        |
-------------------------------------------------+-------------------------

Comment (by jnylen0):

 No worries @fibonaccina!  I also didn't link [40037] back to this ticket
 properly, which didn't help.  Also, I missed something here:  you are
 writing tests against `WP_Query` directly, whereas mine apply to the REST
 API posts controller.  So, while a similar approach may be worth
 considering for `WP_Query`, your latest test cases look totally fine and
 that can be done later in another ticket.

 There is an additional wrinkle which makes this change less
 straightforward:
 [https://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters
 the default order of WP_Query is DESC].  This is not currently respected
 when `orderby` is `post__in`, `post_parent__in`, or `post_name__in`.

 With that in mind, here are the changes that would be needed in the
 `WP_Query` code:

 - Look at the `order` parameter somewhere around
 [https://core.trac.wordpress.org/browser/tags/4.7.2/src/wp-includes/class-
 wp-query.php#L1713 #L1713] and use it to set a variable like
 `$reverse_orderby_array`.  Unlike the rest of `WP_Query`, treat `DESC`
 differently than missing or empty.  (Note we need to do this before
 [https://core.trac.wordpress.org/browser/tags/4.7.2/src/wp-includes/class-
 wp-query.php?marks=2141-2145#L2140 #L2141] because we set the default
 order to `DESC` there.)
 - If we need to reverse an array of IDs/slugs, '''and''' we are actually
 ordering by something that uses one of these arrays, there are three
 places where we need to do so:
 [https://core.trac.wordpress.org/browser/tags/4.7.2/src/wp-includes/class-
 wp-query.php?marks=1945,1957,1967#L1943 #L1945,1957,1967].

 There is one more problem:  the `order` parameter in
 `WP_REST_Posts_Controller`
 [https://core.trac.wordpress.org/browser/tags/4.7.2/src/wp-includes/rest-
 api/endpoints/class-wp-rest-posts-controller.php#L2091 defaults to]
 `desc`, like the rest of `WP_Query`.  So, if we were to make the changes
 described above, then the default order of the results from a REST API
 request would end up being the reverse of the specified `include` array.

 Given all that, and given that it's pretty easy for clients to specify the
 `include` or `post__in` array corresponding to the order in which they
 actually want to receive their results, I'm no longer convinced this
 change is worth it.  Maybe we should just document the existing behavior
 instead, as it's arguably more reasonable than any of the alternatives.
 (I also notice that `post_parent__in` is missing from the `orderby`
 documentation - are there any others?)

 Regardless of what we decide here, though, I am all in favor of adding
 test cases to improve coverage.

--
Ticket URL: <https://core.trac.wordpress.org/ticket/39055#comment:13>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list