I would like to get all the comments for a group of post (not only one post.
I tried
$comment_args = array('number' => '14', 'post_id' => '10,20,30,40' );
$comments = get_comments($comment_args);
foreach($comments as $comment) ...
But does not work. Any ideas?
The
'post_id'
is converted to a positive integer inWP_Comment_Query
, so you cannot successful pass anything else toget_comments()
.You have to filter
'comments_clauses'
. Here you can change theWHERE
clause to usecomment_post_ID IN ( $ids )
instead ofcomment_post_ID = $id
.I would use a static class as a wrapper for
get_comments()
.Sample code
Usage example
Unfortunately,
get_comments()
doesn’t quite work that way. If you specify a post ID, the function will only return comments for that post. The post ID parameter does not accept multiple IDs.However, you could write a function to recursively fetch comments from an array of posts and use that instead:
Once you have the array inside your function, you can order it however you need and limit it to only 14 or so comments … it’s up to you.