Exclude post_type from admin comments_list

The question:

I’m trying not to show comments from a certain custom post type int the comments list on the admin interface.

I went to the “wp-admin/includes/class-wp-comments-list-table.php” and tried to play with the $args array which is passed into the “get_comments” function in order to feed the comments list table.

If I add a post_type parameter there I can filter the comments in order to only see comments from that post type.
But what I want is the opposite: I would like to see all comments but those from one specific post type.

I tried several things like:

1) create a white list by adding all the post types I need but the one I want to exclude. The problem is that it only shows the first type of the array

$args = array(
    ...,
    'post_type' => array('aaa','bbb','ccc','ddd')
);

2) I tried to create a black list by excluding the post type I need. It just doesn’t consider the filter and shows comments from all post types.

$args = array(
    ...,
    'exclude' => array('post_type'=>'xxx')
);

//OR 

$posts_to_exclude=get_posts(array('post_type'=> 'xxx'));
foreach ($posts_to_exclude as $single)
{
    $target[] = $single->ID;   
};
$args = array(
    ...,
    'posts__not_in' => $target
);

Nothing worked.
I just don’t know what else to do.
What concerns me is that the white list seems to be ok according to what I can read over the internet but for some reason it only shows me the first element of the post_type array (and I’m sure I have comments for all posts as they all show up when I don’t filter the comments list table.

Thank you for your help.

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

You’ll need to filter comments_clauses, since WP_Comment_Query only supports a limited post type == X argument.

/**
 * Exclude comments of the "foobar" post type.
 *
 * @param  array  $clauses
 * @param  object $wp_comment_query
 * @return array
 */
function wpse_72210_comments_exclude_post_type( $clauses, $wp_comment_query )
{
    global $wpdb;

    if ( ! $clauses['join'] )
        $clauses['join'] = "JOIN $wpdb->posts ON $wpdb->posts.ID = $wpdb->comments.comment_post_ID";

    if ( ! $wp_comment_query->query_vars['post_type' ] ) // only apply if post_type hasn't already been queried
        $clauses['where'] .= $wpdb->prepare( " AND {$wpdb->posts}.post_type != %s", 'foobar' );

    return $clauses;
}

/**
 * Delay hooking our clauses filter to ensure it's only applied when needed.
 */
function wpse_72210_comments_exclude_lazy_hook( $screen )
{
    if ( $screen->id == 'edit-comments' )
        add_filter( 'comments_clauses', 'wpse_72210_comments_exclude_post_type', 10, 2 );
}

add_action( 'current_screen', 'wpse_72210_comments_exclude_lazy_hook', 10, 2 );


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment