diff --git a/src/wp-includes/comment.php b/src/wp-includes/comment.php index dd45821c34..33a6121fd2 100644 --- a/src/wp-includes/comment.php +++ b/src/wp-includes/comment.php @@ -245,6 +245,7 @@ class WP_Comment_Query { $defaults = array( 'author_email' => '', + 'fields' => '', 'ID' => '', 'karma' => '', 'number' => '', @@ -368,8 +369,16 @@ class WP_Comment_Query { if ( $this->query_vars['count'] ) { $fields = 'COUNT(*)'; } else { - $fields = '*'; + switch ( strtolower( $this->query_vars['fields'] ) ) { + case 'ids': + $fields = "$wpdb->comments.comment_ID"; + break; + default: + $fields = "*"; + break; + } } + $join = ''; $where = $approved; @@ -460,6 +469,12 @@ class WP_Comment_Query { if ( $this->query_vars['count'] ) { return $wpdb->get_var( $query ); } + + if ( 'ids' == $this->query_vars['fields'] ) { + $this->comments = $wpdb->get_col( $query ); + return array_map( 'intval', $this->comments ); + } + $results = $wpdb->get_results( $query ); /** * Filter the comment query results. diff --git a/tests/phpunit/tests/comment/query.php b/tests/phpunit/tests/comment/query.php index 2dc3623608..d2a5e91fd3 100644 --- a/tests/phpunit/tests/comment/query.php +++ b/tests/phpunit/tests/comment/query.php @@ -180,4 +180,22 @@ class Tests_Comment_Query extends WP_UnitTestCase { $this->assertEquals( $users[1], $comments[2]->user_id ); } + + /** + * @ticket 28434 + */ + function test_fields_ids_query() { + $comment_1 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'user_id' => 7, 'comment_approved' => '1' ) ); + $comment_2 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'user_id' => 1, 'comment_approved' => '1' ) ); + $comment_3 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'user_id' => 1, 'comment_approved' => '1' ) ); + + // Ensure we are dealing with integers, and not objects. + $this->assertInternalType( 'integer', $comment_1 ); + $this->assertInternalType( 'integer', $comment_2 ); + $this->assertInternalType( 'integer', $comment_3 ); + + $comment_ids = get_comments( array( 'fields' => 'ids' ) ); + $this->assertCount( 3, $comment_ids ); + $this->assertEquals( array( $comment_1, $comment_2, $comment_3 ), $comment_ids ); + } }