From 333083debe00a3ae751263ca4c8c383dfbda3e6f Mon Sep 17 00:00:00 2001 From: Scott Taylor Date: Thu, 22 May 2014 18:19:13 +0000 Subject: [PATCH] Apply `order` to each passed value for `orderby` in `WP_Query`: * Since `orderby` in `WP_Query` can accept space-delimited sets, yet only one `order` value: when multiple values are passed (and `DESC` is the order), the default sort order `ASC` is being applied to all values before the last in the set. * There is a unit test that sporadically fails since 3.6 in `tests/post/revision` due to multiple posts having the same `post_date` from being added so rapidly * When ordering revisions in `wp_get_post_revisions()`, order by `post_date ID` * Change the `order` value in `wp_get_post_revisions()` to `ASC`. This will produce SQL like: `ORDER BY $wpdb->posts.post_date ASC, $wpdb->posts.ID ASC`. Previously, this would have produced SQL like: `ORDER BY $wpdb->posts.post_date DESC`, and with the addition of ` ID`: `ORDER BY $wpdb->posts.post_date ASC, $wpdb->posts.ID DESC`. Clearly, wrong. The original SQL produced: `ORDER BY $wpdb->posts.post_date DESC`. As such, return the reversions in reverse order using `array_reverse()`. Not doing so would break "Preview Changes." * Add unit tests to assert that all of this works. * All existing unit tests pass with the change to ordering multiple `orderby`s in `WP_Query`. * In the future, we should support independent `order` for each `orderby`, see #17065. Props SergeyBiryukov, wonderboymusic. Fixes #26042. Built from https://develop.svn.wordpress.org/trunk@28541 git-svn-id: http://core.svn.wordpress.org/trunk@28367 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/query.php | 2 +- wp-includes/revision.php | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/wp-includes/query.php b/wp-includes/query.php index 908207ff3e..7216ca0f53 100644 --- a/wp-includes/query.php +++ b/wp-includes/query.php @@ -2668,7 +2668,7 @@ class WP_Query { $orderby_array[] = $orderby; } - $orderby = implode( ',', $orderby_array ); + $orderby = implode( ' ' . $q['order'] . ', ', $orderby_array ); if ( empty( $orderby ) ) $orderby = "$wpdb->posts.post_date ".$q['order']; diff --git a/wp-includes/revision.php b/wp-includes/revision.php index 1203d78105..52412cdb51 100644 --- a/wp-includes/revision.php +++ b/wp-includes/revision.php @@ -417,7 +417,7 @@ function wp_get_post_revisions( $post_id = 0, $args = null ) { if ( ! $post || empty( $post->ID ) ) return array(); - $defaults = array( 'order' => 'DESC', 'orderby' => 'date', 'check_enabled' => true ); + $defaults = array( 'order' => 'ASC', 'orderby' => 'date ID', 'check_enabled' => true ); $args = wp_parse_args( $args, $defaults ); if ( $args['check_enabled'] && ! wp_revisions_enabled( $post ) ) @@ -428,6 +428,8 @@ function wp_get_post_revisions( $post_id = 0, $args = null ) { if ( ! $revisions = get_children( $args ) ) return array(); + $revisions = array_reverse( $revisions ); + return $revisions; }