Skip to main content
6 of 7
added 36 characters in body
birgire
  • 67.8k
  • 7
  • 119
  • 251

It looks like WP_Comment_Query() only supports a single post type.

You can adjust it, by using the comments_clauses filter.

Try for example:

$defaults = array(
    'number'    => 5,
    'post_type' => array( 'post','authors','movies' ),
);
add_filter( 'comments_clauses', 'wpse_121051', 10, 2 );     
$comments = get_comments($defaults)

where

 /**
  * Support for multiple post types for comments
  *
  * @param array $clauses
  * @param object $wpqc WP_Comment_Query
  * @return array $clauses
  */   
 function wpse_121051( $clauses, $wpqc )
 {
    global $wpdb;
    
    // Remove the comments_clauses filter, we don't need it anymore. 
    remove_filter( current_filter(), __FUNCTION__ );

    // Add the multiple post type support.
    if( isset( $wpqc->query_vars['post_type'][0] ) )
    {
        $from = "$wpdb->posts.post_type = '" . $wpqc->query_vars['post_type'][0] . "'";
        $to   = "$wpdb->posts.post_type IN ( '" . join( "', '", $wpqc->query_vars['post_type'] ) . "' ) ";

        $clauses['where'] = str_replace( $from, $to, $clauses['where'] );
    }  

    return $clauses;
 }

The plugin:

As kindly suggested by @kaiser, I made a small plugin to add multiple post types support to WP_Comment_Query() and get_comments(). Let's hope this missing feature will be supported by the WordPress core in the near future ;-)

birgire
  • 67.8k
  • 7
  • 119
  • 251