Opened 15 months ago

Last modified 9 months ago

#20006 new defect (bug)

get_comments (and WP_Comment_Query) does not accept multiple post_type

Reported by: Justin_K Owned by:
Priority: normal Milestone: Awaiting Review
Component: Comments Version: 3.3.1
Severity: normal Keywords: has-patch
Cc:

Description

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 (1)

get_comments.patch (1.6 KB) - added by nprasath002 15 months ago.
Support array of args

Download all attachments as: .zip

Change History (4)

Support array of args

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

  • Keywords has-patch added
Note: See TracTickets for help on using tickets.