From 4482d000ba0e04ba5ebbc6a80f01ed0da41d439c Mon Sep 17 00:00:00 2001 From: Boone Gorges Date: Sun, 26 Oct 2014 16:45:37 +0000 Subject: [PATCH] Fix 'count' in `WP_Comment_Query` when using 'meta_query'. Props heshiming, desaiuditd. Fixes #23369. git-svn-id: https://develop.svn.wordpress.org/trunk@30026 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/comment.php | 5 +++- tests/phpunit/tests/comment/query.php | 36 +++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/comment.php b/src/wp-includes/comment.php index c5776512c0..8de3608f93 100644 --- a/src/wp-includes/comment.php +++ b/src/wp-includes/comment.php @@ -559,7 +559,10 @@ class WP_Comment_Query { $clauses = $this->meta_query->get_sql( 'comment', $wpdb->comments, 'comment_ID', $this ); $join .= $clauses['join']; $where .= $clauses['where']; - $groupby = "{$wpdb->comments}.comment_ID"; + + if ( ! $this->query_vars['count'] ) { + $groupby = "{$wpdb->comments}.comment_ID"; + } } $date_query = $this->query_vars['date_query']; diff --git a/tests/phpunit/tests/comment/query.php b/tests/phpunit/tests/comment/query.php index db171759a0..b857e46784 100644 --- a/tests/phpunit/tests/comment/query.php +++ b/tests/phpunit/tests/comment/query.php @@ -613,4 +613,40 @@ class Tests_Comment_Query extends WP_UnitTestCase { $this->assertNotContains( 'ORDER BY', $q->request ); } + + public function test_count() { + $c1 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'user_id' => 7 ) ); + $c2 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'user_id' => 7 ) ); + + $q = new WP_Comment_Query(); + $found = $q->query( array( + 'count' => true, + ) ); + + $this->assertEquals( 2, $found ); + } + + /** + * @ticket 23369 + */ + public function test_count_with_meta_query() { + $c1 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'user_id' => 7 ) ); + $c2 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'user_id' => 7 ) ); + $c3 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'user_id' => 7 ) ); + add_comment_meta( $c1, 'foo', 'bar' ); + add_comment_meta( $c3, 'foo', 'bar' ); + + $q = new WP_Comment_Query(); + $found = $q->query( array( + 'count' => true, + 'meta_query' => array( + array( + 'key' => 'foo', + 'value' => 'bar', + ), + ), + ) ); + + $this->assertEquals( 2, $found ); + } }