Make WordPress Core


Ignore:
Timestamp:
07/08/2014 05:15:53 PM (10 years ago)
Author:
wonderboymusic
Message:

Allow an array() to be passed as the value for orderby to WP_Query. Allows for an independent order value for each key.

Example: 'orderby' => array( 'title' => 'DESC', 'menu_order' => 'ASC' ).

Adds docs and unit tests.

Props wonderboymusic, johnbillion, DrewAPicture, dd32, andy.
See #17065.

File:
1 edited

Legend:

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

    r28622 r29027  
    762762        $this->assertNotContains( "post_status <> 'auto-draft'", $q3->request );
    763763    }
     764
     765    /**
     766     *
     767     * @ticket 17065
     768     */
     769    function test_orderby_array() {
     770        global $wpdb;
     771
     772        $q1 = new WP_Query( array(
     773            'orderby' => array(
     774                'type' => 'DESC',
     775                'name' => 'ASC'
     776            )
     777        ) );
     778        $this->assertContains(
     779            "ORDER BY $wpdb->posts.post_type DESC, $wpdb->posts.post_name ASC",
     780            $q1->request
     781        );
     782
     783        $q2 = new WP_Query( array( 'orderby' => array() ) );
     784        $this->assertNotContains( 'ORDER BY', $q2->request );
     785        $this->assertNotContains( 'ORDER', $q2->request );
     786
     787        $q3 = new WP_Query( array( 'post_type' => 'post' ) );
     788        $this->assertContains(
     789            "ORDER BY $wpdb->posts.post_date DESC",
     790            $q3->request
     791        );
     792
     793        $q4 = new WP_Query( array( 'post_type' => 'post' ) );
     794        $this->assertContains(
     795            "ORDER BY $wpdb->posts.post_date DESC",
     796            $q4->request
     797        );
     798    }
     799
     800    /**
     801     *
     802     * @ticket 17065
     803     */
     804    function test_order() {
     805        global $wpdb;
     806
     807        $q1 = new WP_Query( array(
     808            'orderby' => array(
     809                'post_type' => 'foo'
     810            )
     811        ) );
     812        $this->assertContains(
     813            "ORDER BY $wpdb->posts.post_type DESC",
     814            $q1->request
     815        );
     816
     817        $q2 = new WP_Query( array(
     818            'orderby' => 'title',
     819            'order'   => 'foo'
     820        ) );
     821        $this->assertContains(
     822            "ORDER BY $wpdb->posts.post_title DESC",
     823            $q2->request
     824        );
     825
     826        $q3 = new WP_Query( array(
     827            'order' => 'asc'
     828        ) );
     829        $this->assertContains(
     830            "ORDER BY $wpdb->posts.post_date ASC",
     831            $q3->request
     832        );
     833    }
    764834}
Note: See TracChangeset for help on using the changeset viewer.