Get comments for more than one post

I would like to get all the comments for a group of post (not only one post.

I tried

Read More
$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?

Related posts

Leave a Reply

2 comments

  1. The 'post_id' is converted to a positive integer in WP_Comment_Query, so you cannot successful pass anything else to get_comments().

    You have to filter 'comments_clauses'. Here you can change the WHERE clause to use comment_post_ID IN ( $ids ) instead of comment_post_ID = $id.

    I would use a static class as a wrapper for get_comments().

    Sample code

    /**
     * Query comments for multiple post IDs.
     */
    class T5_Multipost_Comments
    {
        /**
         * Post IDs, eg. array ( 1, 2, 40 )
         * @var array
         */
        protected static $post_ids = array ();
    
        /**
         * Called like get_comments.
         *
         * @param  array $args
         * @param  array $post_ids
         * @return array
         */
        public static function get( $args = array (), $post_ids = array () )
        {
            if ( array () !== $post_ids )
            {
                self::$post_ids = $post_ids;
                add_filter( 'comments_clauses', array ( __CLASS__, 'filter_where_clause' ) );
            }
            return get_comments( $args );
        }
    
        /**
         * Filter the comment query
         *
         * @param array $q Query parts, see WP_Comment_Query::query()
         *
         * @return array
         */
        public static function filter_where_clause( $q )
        {
            $ids       = implode( ', ', self::$post_ids );
            $_where_in = " AND comment_post_ID IN ( $ids )";
    
            if ( FALSE !== strpos( $q['where'], ' AND comment_post_ID =' ) )
            {
                $q['where'] = preg_replace(
                    '~ AND comment_post_ID = d+~',
                    $_where_in,
                    $q['where']
                );
            }
            else
            {
                $q['where'] .= $_where_in;
            }
    
            remove_filter( 'comments_clauses', array ( __CLASS__, 'filter_where_clause' ) );
            return $q;
        }
    }
    

    Usage example

    $multi_comments = T5_Multipost_Comments::get(
        array ( 'number' => '14' ), // comment args
        array ( 149, 564, 151 )     // post IDs
    );
    print '<pre>' . htmlspecialchars( print_r( $multi_comments, TRUE ) ) . '</pre>';
    
  2. 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:

    get_comments_from_range( $post_ids ) {
        foreach( $post_ids as $post_id ) {
            $comment_collection[] = get_comments( array( 'post_id' => $post_id ) );
        }
    
        return $comment_collection;
    }
    
    $comments = get_comments_from_range( array( '10', '20', '30', '40' ) );
    

    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.