Skip to main content
added 89 characters in body
Source Link
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] ) )
    { 

        $join = join( "', '", array_map( 'esc_sql', $wpqc->query_vars['post_type'] ) );

        $from = "$wpdb->posts.post_type = '" . $wpqc->query_vars['post_type'][0] . "'";                         
        $to   = sprintf( "$wpdb->posts.post_type IN ( '" . join( "', '", $wpqc->query_vars['post_type']'%s' ) .", "'$join ) ";;

        $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 ;-)

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 ;-)

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] ) )
    { 

        $join = join( "', '", array_map( 'esc_sql', $wpqc->query_vars['post_type'] ) );

        $from = "$wpdb->posts.post_type = '" . $wpqc->query_vars['post_type'][0] . "'";                         
        $to   = sprintf( "$wpdb->posts.post_type IN ( '%s' ) ", $join );

        $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 ;-)

added 36 characters in body
Source Link
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 Here is thesmall 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 ;-)

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:

Here is the plugin to add multiple post types support to WP_Comment_Query() and get_comments():

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 ;-)

added 206 characters in body
Source Link
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:

Here is the plugin to add multiple post types support to WP_Comment_Query() and get_comments():

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;
 }

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:

Here is the plugin to add multiple post types support to WP_Comment_Query() and get_comments():

added 15 characters in body
Source Link
birgire
  • 67.8k
  • 7
  • 119
  • 251
Loading
added 99 characters in body
Source Link
birgire
  • 67.8k
  • 7
  • 119
  • 251
Loading
added 55 characters in body
Source Link
birgire
  • 67.8k
  • 7
  • 119
  • 251
Loading
Source Link
birgire
  • 67.8k
  • 7
  • 119
  • 251
Loading