Make WordPress Core

Opened 6 years ago

Closed 3 years ago

#20006 closed defect (bug) (fixed)

get_comments (and WP_Comment_Query) does not accept multiple post_type

Reported by: Justin_K Owned by: boonebgorges
Milestone: 4.2 Priority: normal
Severity: normal Version: 3.1
Component: Comments Keywords: has-patch good-first-bug dev-feedback
Focuses: Cc:


Although get_comments was recently improved to support post_types, it does not seem to properly handle MULTIPLE post_types.

Suppose you need to get the most recent comments for posts, pages, and attachments (attachments don't seem to be included by default). The following doesn't work, as it only returns comments on attachments (or whichever status is listed first):

get_comments(array('number'=>20, 'status'=>'approve', 'post_type'=>array('attachment','post','page')));

The following doesn't work either, only returning the default post/page comments (taken from the example for multiple category handling at http://codex.wordpress.org/Class_Reference/WP_Query):

get_comments(array('number'=>20, 'status'=>'approve', 'post_type__in'=>array('attachment','post','page')));

A quick look at wp-includes/comment.php, line 327 (WP v3.3.1) shows that the 2nd option will be array_filter'ed away. Immediately below, all of the WHERE clauses are hardcoded as "=", which is why there's no way to specify more than one. As a result, it seems like the only way to get recent comments on posts,pages,and attachments (or any other subset of >1 post_type) is to do multiple calls to get_comments (i.e. one with default params and one for attachments) then merge the results together...effectively requiring twice the work.

Attachments (3)

get_comments.patch (1.6 KB) - added by nprasath002 6 years ago.
Support array of args
20006.2.patch (1.0 KB) - added by c3mdigital 5 years ago.
Allow array of post_types (refresh of original patch)
20006.3.patch (3.5 KB) - added by ianmjones 3 years ago.
This patch does not use foreach and includes unit tests.

Download all attachments as: .zip

Change History (14)

6 years ago

Support array of args

#1 @nprasath002
6 years ago

Now the args can be passed as an array

'post_type' => array('attachment', 'post', 'page')

I have added another foreach loop inside the current one to add OR statements.
First time i have wrote a patch involving a query class.
Glad if someone could give me some feedback

#2 @nprasath002
6 years ago

  • Keywords has-patch added

#4 @c3mdigital
5 years ago

  • Keywords needs-refresh added
  • Version changed from 3.3.1 to 3.1

5 years ago

Allow array of post_types (refresh of original patch)

#5 @c3mdigital
5 years ago

  • Keywords needs-refresh removed

#7 @boonebgorges
3 years ago

  • Keywords needs-refresh good-first-bug needs-unit-tests added

We can do this. Needs a refresh and some unit tests.

3 years ago

This patch does not use foreach and includes unit tests.

#8 @ianmjones
3 years ago

  • Keywords needs-refresh removed

#9 @valendesigns
3 years ago

  • Keywords dev-feedback added; needs-unit-tests removed

Tested 20006.3.patch running 4.2-alpha-31007-src. All tests pass and the code looks solid.

#10 @boonebgorges
3 years ago

  • Milestone changed from Awaiting Review to 4.2

Looks good. I'm going to split up the tests, and simplify the logic a bit.

#11 @boonebgorges
3 years ago

  • Owner set to boonebgorges
  • Resolution set to fixed
  • Status changed from new to closed

In 31015:

Support array values for post-related parameters in WP_Comment_Query.

Props nprasath002, c3mdigital, ianmjones.
Fixes #20006.

Note: See TracTickets for help on using tickets.