This may sound like an unusual request but I am try to display comments from several related posts in a single comment template.

For example I have several posts on the same topic (IDs 253, 724, 798) and I want all comments from these posts to appear next in a continuous thread. So if I am viewing post ID 724 I can also see the comments from posts 253 and 798.

Can I pass multiple post IDs to the comments template to achieve this does this require a custom query before or after the comments template to show all the comments in a merged thread?


you can get comments from each post by its id with

$comments253 = get_comments('post_id=253');
$comments724 = get_comments('post_id=724');
$comments798 = get_comments('post_id=798');

then merge ( array-merge ) and sort the array by date ( comment->comment_date being the key to the date value) if you want. then just

 foreach($comments as $comment) :
      echo($comment->comment_author . '<br />' . $comment->comment_content);

This is all very manual, and you might want to automatize the process, but that's probably an different matter.


Getting the comments for multiple posts is rather simple: Just replace comment_post_ID = YOUR_POST_ID with an IN() function.

function wpse_59687_multiple_comment_post_id_query_filter( $query )
    $post_ids = array ( 149, 188, 151 );
    if ( FALSE === strpos( $query, 'comment_post_ID = ' ) )
        return $query; // not the query we want to filter

    remove_filter( 'query', 'wpse_59687_multiple_comment_post_id_query_filter' );

    $replacement = 'comment_post_ID IN(' . implode( ',', $post_ids ) . ')';
    return preg_replace( '~comment_post_ID = \d+~', $replacement, $query );

Now you call this function just before you call comments_template():

add_filter( 'query', 'wpse_59687_multiple_comment_post_id_query_filter' );

What’s more difficult:

  • get_comments_number() is wrong. You have to filter its value too.
  • Replies to a comment from another post are redirected to the URL of the other post, not the page where the commenter has written the comment. Fixing that will be not so trivial …
  • Unsure if answer outdated, but on my installation it replaces the wrong place, i.e. SELECT * FROM 0_comments LEFT JOIN 0_posts ON comment_post_ID IN(4546,4538)_posts.ID WHERE . See for more info: laravel.io/bin/nQXex – FooBar May 5 '15 at 8:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.