WordPress.org

Make WordPress Core


Ignore:
Timestamp:
05/22/2014 06:18:11 PM (5 years ago)
Author:
wonderboymusic
Message:

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 orderbys in WP_Query.
  • In the future, we should support independent order for each orderby, see #17065.

Props SergeyBiryukov, wonderboymusic.
Fixes #26042.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/post/revisions.php

    r25002 r28541  
    339339        }
    340340    }
     341
     342    /**
     343     * @ticket 26042
     344     */
     345    function test_wp_get_posts_revisions_sql() {
     346        $post = get_default_post_to_edit( 'post', true );
     347
     348        add_filter( 'query', array( $this, '_filter_query' ) );
     349
     350        wp_get_post_revisions( $post->ID );
     351    }
     352
     353    function _filter_query( $sql ) {
     354        remove_filter( 'query', array( $this, '_filter_query' ) );
     355        global $wpdb;
     356        $this->assertContains( "ORDER BY $wpdb->posts.post_date ASC, $wpdb->posts.ID ASC", $sql );
     357        return $sql;
     358    }
     359
     360    /**
     361     * @ticket 26042
     362     */
     363    function test_revision_order() {
     364        $ok = 0;
     365        $reversed = 0;
     366
     367        for ( $i = 0; $i < 100; $i++ ) {
     368            $post_id = $this->factory->post->create( array( 'post_title' => 'some-post', 'post_type' => 'post', 'post_content' => 'some_content' ) );
     369
     370            for ( $j = 1; $j < 3; $j++ ) {
     371                wp_update_post( array( 'post_content' => 'updated post' . $j , 'ID' => $post_id ) );
     372            }
     373
     374            $revisions = wp_get_post_revisions( $post_id );
     375            $first = array_shift( $revisions );
     376            $last = array_pop( $revisions );
     377
     378            if ( $first->ID < $last->ID ) {
     379                $reversed++;
     380            } else {
     381                $ok++;
     382            }
     383        }
     384
     385        $this->assertEquals( 100, $ok );
     386        $this->assertEquals( 0, $reversed );
     387    }
    341388}
Note: See TracChangeset for help on using the changeset viewer.