Opened 13 years ago
Closed 10 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: |
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 (3)
Change History (14)
#1
@
13 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
#7
@
10 years ago
- Keywords needs-refresh good-first-bug needs-unit-tests added
We can do this. Needs a refresh and some unit tests.
#9
@
10 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.
Support array of args