Make WordPress Core

Ticket #11398: 11398.2.diff

File 11398.2.diff, 3.2 KB (added by wonderboymusic, 13 years ago)
  • tests/tests/query/results.php

     
    369369                        'child-two',
    370370                ), wp_list_pluck( $posts, 'post_title' ) );
    371371        }
     372
     373        /**
     374         * @ticket 11398
     375         */
     376        function test_query_orderby_comment_date() {
     377                $post_date = '2013-08-02 14:15:04';
     378                $one = $this->factory->post->create( array( 'post_title' => 'one', 'post_date' => $post_date ) );
     379                $two = $this->factory->post->create( array( 'post_title' => 'two', 'post_date' => $post_date ) );
     380                $three = $this->factory->post->create( array( 'post_title' => 'three', 'post_date' => $post_date ) );
     381                $four = $this->factory->post->create( array( 'post_title' => 'four', 'post_date' => $post_date ) );
     382
     383                $this->factory->comment->create_post_comments( $three, 1, array( 'comment_date' => '2013-08-02 14:15:05' ) );
     384                $this->factory->comment->create_post_comments( $two, 1, array( 'comment_date' => '2013-08-02 14:15:06' ) );
     385                $this->factory->comment->create_post_comments( $one, 1, array( 'comment_date' => '2013-08-02 14:15:07' ) );
     386
     387                $asc_posts = $this->q->query( array(
     388                        'post__in' => array( $one, $two, $three, $four ),
     389                        'orderby' => 'comment_date',
     390                        'order' => 'ASC'
     391                ) );
     392
     393                $this->assertEquals( array(
     394                        'three',
     395                        'two',
     396                        'one',
     397                        'four',
     398                ), wp_list_pluck( $asc_posts, 'post_title' ) );
     399
     400                $desc_posts = $this->q->query( array(
     401                        'post__in' => array( $one, $two, $three, $four ),
     402                        'orderby' => 'comment_date',
     403                        'order' => 'DESC'
     404                ) );
     405
     406                $this->assertEquals( array(
     407                        'one',
     408                        'two',
     409                        'three',
     410                        'four',
     411                ), wp_list_pluck( $desc_posts, 'post_title' ) );
     412        }
    372413}
  • src/wp-includes/query.php

    Property changes on: tests/data
    ___________________________________________________________________
    Added: svn:ignore
       + .trac-ticket-cache.core.trac.wordpress.org
    .trac-ticket-cache.unit-tests.trac.wordpress.org
    
    
    
    Property changes on: src
    ___________________________________________________________________
    Added: svn:ignore
       + .wp-tests-version
    
    
     
    23612361                        $orderby = "FIELD( {$wpdb->posts}.post_parent, $post_parent__in )";
    23622362                } else {
    23632363                        // Used to filter values
    2364                         $allowed_keys = array('name', 'author', 'date', 'title', 'modified', 'menu_order', 'parent', 'ID', 'rand', 'comment_count');
     2364                        $allowed_keys = array( 'name', 'author', 'date', 'title', 'modified', 'menu_order', 'parent', 'ID', 'rand', 'comment_count', 'comment_date' );
    23652365                        if ( !empty($q['meta_key']) ) {
    23662366                                $allowed_keys[] = $q['meta_key'];
    23672367                                $allowed_keys[] = 'meta_value';
     
    23962396                                        case 'comment_count':
    23972397                                                $orderby = "$wpdb->posts.comment_count";
    23982398                                                break;
     2399                                        case 'comment_date':
     2400                                                $orderby = "ISNULL($wpdb->comments.comment_date), $wpdb->comments.comment_date";
     2401                                                $join .= " LEFT JOIN $wpdb->comments ON $wpdb->posts.ID = $wpdb->comments.comment_post_ID ";
     2402                                                break;
    23992403                                        default:
    24002404                                                $orderby = "$wpdb->posts.post_" . $orderby;
    24012405                                }