WordPress.org

Make WordPress Core

Changeset 30004


Ignore:
Timestamp:
10/24/2014 02:50:24 AM (7 years ago)
Author:
boonebgorges
Message:

Allow ORDER BY in WP_Comment_Query::query() to be disabled.

Disable ORDER BY by passing boolean false, an empty array, or the string
'none' to the 'orderby parameter. This mirrors the behavior of WP_Query.

Props psycleuk.
Fixes #29902.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/comment.php

    r30003 r30004  
    388388        $order = ( 'ASC' == strtoupper( $this->query_vars['order'] ) ) ? 'ASC' : 'DESC';
    389389
    390         if ( ! empty( $this->query_vars['orderby'] ) ) {
     390        // Disable ORDER BY with 'none', an empty array, or boolean false.
     391        if ( in_array( $this->query_vars['orderby'], array( 'none', array(), false ), true ) ) {
     392            $orderby = '';
     393        } else if ( ! empty( $this->query_vars['orderby'] ) ) {
    391394            $ordersby = is_array( $this->query_vars['orderby'] ) ?
    392395                $this->query_vars['orderby'] :
     
    589592        }
    590593
    591         $this->request = "SELECT $fields FROM $wpdb->comments $join WHERE $where $groupby $orderby $order $limits";
     594        if ( $orderby ) {
     595            $orderby = "ORDER BY $orderby $order";
     596        }
     597
     598        $this->request = "SELECT $fields FROM $wpdb->comments $join WHERE $where $groupby $orderby $limits";
    592599
    593600        if ( $this->query_vars['count'] ) {
  • trunk/tests/phpunit/tests/comment/query.php

    r30003 r30004  
    578578        $this->assertContains( 'ORDER BY comment_date_gmt', $q->request );
    579579    }
     580
     581    /**
     582     * @ticket 29902
     583     */
     584    public function test_orderby_none() {
     585        $q = new WP_Comment_Query();
     586        $q->query( array(
     587            'orderby' => 'none',
     588        ) );
     589
     590        $this->assertNotContains( 'ORDER BY', $q->request );
     591    }
     592
     593    /**
     594     * @ticket 29902
     595     */
     596    public function test_orderby_empty_array() {
     597        $q = new WP_Comment_Query();
     598        $q->query( array(
     599            'orderby' => array(),
     600        ) );
     601
     602        $this->assertNotContains( 'ORDER BY', $q->request );
     603    }
     604
     605    /**
     606     * @ticket 29902
     607     */
     608    public function test_orderby_false() {
     609        $q = new WP_Comment_Query();
     610        $q->query( array(
     611            'orderby' => false,
     612        ) );
     613
     614        $this->assertNotContains( 'ORDER BY', $q->request );
     615    }
    580616}
Note: See TracChangeset for help on using the changeset viewer.