Index: tests/tests/query/results.php
===================================================================
--- tests/tests/query/results.php	(revision 25101)
+++ tests/tests/query/results.php	(working copy)
@@ -369,4 +369,45 @@
 			'child-two',
 		), wp_list_pluck( $posts, 'post_title' ) );
 	}
+
+	/**
+	 * @ticket 11398
+	 */
+	function test_query_orderby_comment_date() {
+		$post_date = '2013-08-02 14:15:04';
+		$one = $this->factory->post->create( array( 'post_title' => 'one', 'post_date' => $post_date ) );
+		$two = $this->factory->post->create( array( 'post_title' => 'two', 'post_date' => $post_date ) );
+		$three = $this->factory->post->create( array( 'post_title' => 'three', 'post_date' => $post_date ) );
+		$four = $this->factory->post->create( array( 'post_title' => 'four', 'post_date' => $post_date ) );
+
+		$this->factory->comment->create_post_comments( $three, 1, array( 'comment_date' => '2013-08-02 14:15:05' ) );
+		$this->factory->comment->create_post_comments( $two, 1, array( 'comment_date' => '2013-08-02 14:15:06' ) );
+		$this->factory->comment->create_post_comments( $one, 1, array( 'comment_date' => '2013-08-02 14:15:07' ) );
+
+		$asc_posts = $this->q->query( array(
+			'post__in' => array( $one, $two, $three, $four ),
+			'orderby' => 'comment_date',
+			'order' => 'ASC'
+		) );
+
+		$this->assertEquals( array(
+			'three',
+			'two',
+			'one',
+			'four',
+		), wp_list_pluck( $asc_posts, 'post_title' ) );
+
+		$desc_posts = $this->q->query( array(
+			'post__in' => array( $one, $two, $three, $four ),
+			'orderby' => 'comment_date',
+			'order' => 'DESC'
+		) );
+
+		$this->assertEquals( array(
+			'one',
+			'two',
+			'three',
+			'four',
+		), wp_list_pluck( $desc_posts, 'post_title' ) );
+	}
 }

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


Index: src/wp-includes/query.php
===================================================================
--- src/wp-includes/query.php	(revision 25101)
+++ src/wp-includes/query.php	(working copy)
@@ -2361,7 +2361,7 @@
 			$orderby = "FIELD( {$wpdb->posts}.post_parent, $post_parent__in )";
 		} else {
 			// Used to filter values
-			$allowed_keys = array('name', 'author', 'date', 'title', 'modified', 'menu_order', 'parent', 'ID', 'rand', 'comment_count');
+			$allowed_keys = array( 'name', 'author', 'date', 'title', 'modified', 'menu_order', 'parent', 'ID', 'rand', 'comment_count', 'comment_date' );
 			if ( !empty($q['meta_key']) ) {
 				$allowed_keys[] = $q['meta_key'];
 				$allowed_keys[] = 'meta_value';
@@ -2396,6 +2396,10 @@
 					case 'comment_count':
 						$orderby = "$wpdb->posts.comment_count";
 						break;
+					case 'comment_date':
+						$orderby = "ISNULL($wpdb->comments.comment_date), $wpdb->comments.comment_date";
+						$join .= " LEFT JOIN $wpdb->comments ON $wpdb->posts.ID = $wpdb->comments.comment_post_ID ";
+						break;
 					default:
 						$orderby = "$wpdb->posts.post_" . $orderby;
 				}
