diff --git a/src/wp-includes/class-wp-comment-query.php b/src/wp-includes/class-wp-comment-query.php index 01048e6a3f..ec35a2adac 100644 --- a/src/wp-includes/class-wp-comment-query.php +++ b/src/wp-includes/class-wp-comment-query.php @@ -94,6 +94,7 @@ class WP_Comment_Query { * * @since 4.2.0 * @since 4.4.0 `$parent__in` and `$parent__not_in` were added. + * @since 4.4.0 Order by `comment__in` was added. * @access public * * @param string|array $query { @@ -130,7 +131,7 @@ class WP_Comment_Query { * 'comment_author_url', 'comment_content', 'comment_date', * 'comment_date_gmt', 'comment_ID', 'comment_karma', * 'comment_parent', 'comment_post_ID', 'comment_type', 'user_id', - * 'meta_value', 'meta_value_num', the value of $meta_key, and the + * 'comment__in', 'meta_value', 'meta_value_num', the value of $meta_key, and the * array keys of `$meta_query`. Also accepts false, an empty array, * or 'none' to disable `ORDER BY` clause. * Default: 'comment_date_gmt'. @@ -390,7 +391,7 @@ class WP_Comment_Query { $_order = $_value; } - if ( ! $found_orderby_comment_ID && 'comment_ID' === $_orderby ) { + if ( ! $found_orderby_comment_ID && in_array( $_orderby, array( 'comment_ID', 'comment__in' ) ) ) { $found_orderby_comment_ID = true; } @@ -400,6 +401,11 @@ class WP_Comment_Query { continue; } + if ( 'comment__in' === $_orderby ) { + $orderby_array[] = $parsed; + continue; + } + $orderby_array[] = $parsed . ' ' . $this->parse_order( $_order ); } @@ -772,6 +778,9 @@ class WP_Comment_Query { $parsed = "$wpdb->commentmeta.meta_value"; } elseif ( $orderby == 'meta_value_num' ) { $parsed = "$wpdb->commentmeta.meta_value+0"; + } elseif ( $orderby == 'comment__in' ) { + $comment__in = implode( ',', array_map( 'absint', $this->query_vars['comment__in'] ) ); + $parsed = "FIELD( {$wpdb->comments}.comment_ID, $comment__in )"; } elseif ( in_array( $orderby, $allowed_keys ) ) { if ( isset( $meta_query_clauses[ $orderby ] ) ) { diff --git a/tests/phpunit/tests/comment/query.php b/tests/phpunit/tests/comment/query.php index 7356541ef4..cecf92cf0a 100644 --- a/tests/phpunit/tests/comment/query.php +++ b/tests/phpunit/tests/comment/query.php @@ -1837,4 +1837,38 @@ class Tests_Comment_Query extends WP_UnitTestCase { $this->assertEqualSets( array( $c1, $c2 ), $ids->comments ); } + + /** + * @ticket 33883 + */ + public function test_orderby_comment__in() { + $this->factory->comment->create( array( + 'comment_post_ID' => $this->post_id, + 'comment_approved' => '1' + ) ); + + $c2 = $this->factory->comment->create( array( + 'comment_post_ID' => $this->post_id, + 'comment_approved' => '1' + ) ); + $c3 = $this->factory->comment->create( array( + 'comment_post_ID' => $this->post_id, + 'comment_approved' => '1' + ) ); + + $this->factory->comment->create( array( + 'comment_post_ID' => $this->post_id, + 'comment_approved' => '1' + ) ); + + + $ids = new WP_Comment_Query( array( + 'fields' => 'ids', + 'comment__in' => array( $c2, $c3 ), + 'orderby' => 'comment__in' + ) ); + + $this->assertEquals( array( $c2, $c3 ), $ids->comments ); + + } }