Make WordPress Core

Opened 11 years ago

Closed 9 years ago

#23369 closed defect (bug) (fixed)

WP_Comment_Query meta query 'count' conflicts with 'group by'

Reported by: heshiming's profile heshiming Owned by: boonebgorges's profile boonebgorges
Milestone: 4.1 Priority: normal
Severity: normal Version: 3.5.1
Component: Comments Keywords: has-patch
Focuses: Cc:


This problem relates to /wp-includes/comment.php, both 3.5.1 and trunk are affected. The following line number refers to the 3.5.1 version.

At line 195, definition of class WP_Comment_Query , a typical meta query will generate such SQL:

SELECT * FROM wp_comments INNER JOIN wp_commentmeta ON (wp_comments.comment_ID = wp_commentmeta.comment_id) WHERE ( comment_approved = '0' OR comment_approved = '1' ) AND comment_post_ID = 22 AND ( (wp_commentmeta.meta_key = 'my_flag' AND CAST(wp_commentmeta.meta_value AS CHAR) = 'my_value') ) GROUP BY wp_comments.comment_ID ORDER BY comment_date_gmt DESC

Note that at line 357, a meta query will always generate a GROUP BY clause. This works for comment rows. Say that I've got 3 comments matching this query, I'll see each of them in a row:

comment row 1, data
comment row 2, data
comment row 3, data

However, this query doesn't work well then $count = true. Note that by specifying $count = true, the function only changes 'SELECT *' into 'SELECT COUNT(*)'. For 3 comment result, I'm seeing something like this:


And you can see, because there's a 'GROUP BY' clause. And each comment has a unique ID. The result is 3 'grouped row' of comments, and each will always have count of 1.

Therefore, the result of $wpdb->get_var() will be either 1, or NULL (when nothing is matched). I believe this behavior is not by intention.

To get count working, 'GROUP BY' clause must be removed when $count is true.

Attachments (1)

23369.patch (556 bytes) - added by desaiuditd 10 years ago.
Patch for the issue ticket #23369

Download all attachments as: .zip

Change History (9)

#1 @SergeyBiryukov
11 years ago

  • Version changed from trunk to 3.5.1

10 years ago

Patch for the issue ticket #23369

#2 @desaiuditd
10 years ago

  • Cc desaiuditd@… added
  • Keywords needs-testing added

#3 @desaiuditd
10 years ago

  • Keywords has-patch dev-feedback added

#4 @desaiuditd
10 years ago

Any updates on this ?
Is it going to get reviewed ?

#5 follow-up: @ocean90
10 years ago

#27442 was marked as a duplicate.

#6 in reply to: ↑ 5 @desaiuditd
10 years ago

Replying to ocean90:

#27442 was marked as a duplicate.

There is no update on this issue since 2 months. I thought it is left behind because of the old version marked. Is there any specific reason it is not being looked into ?

#7 @boonebgorges
9 years ago

  • Keywords needs-testing dev-feedback removed
  • Milestone changed from Awaiting Review to 4.1
  • Owner set to boonebgorges
  • Status changed from new to accepted

#8 @boonebgorges
9 years ago

  • Resolution set to fixed
  • Status changed from accepted to closed

In 30026:

Fix 'count' in WP_Comment_Query when using 'meta_query'.

Props heshiming, desaiuditd.
Fixes #23369.

Note: See TracTickets for help on using tickets.