WordPress.org

Make WordPress Core

Opened 18 months ago

Last modified 4 months ago

#23369 new defect (bug)

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

Reported by: heshiming Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 3.5.1
Component: Comments Keywords: needs-testing has-patch dev-feedback
Focuses: Cc:

Description

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:

1
1
1

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 8 months ago.
Patch for the issue ticket #23369

Download all attachments as: .zip

Change History (7)

comment:1 SergeyBiryukov18 months ago

  • Version changed from trunk to 3.5.1

desaiuditd8 months ago

Patch for the issue ticket #23369

comment:2 desaiuditd8 months ago

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

comment:3 desaiuditd7 months ago

  • Keywords has-patch dev-feedback added

comment:4 desaiuditd6 months ago

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

comment:5 follow-up: ocean904 months ago

#27442 was marked as a duplicate.

comment:6 in reply to: ↑ 5 desaiuditd4 months ago

Replying to ocean90:

#27442 was marked as a duplicate.

@ocean90
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 ?

Note: See TracTickets for help on using tickets.