2

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.

4
  • WordPress uses the class WP_List_Table to query and generate the output for most of the admin table/list displays. So have a look at the documentation at codex.wordpress.org/Class_Reference/WP_List_Table
    – Steve
    Nov 11, 2012 at 15:04
  • Thank you. I don't understand the first part of your answer though as I'm not talking about the wp_list_table (which indeed feeds the most of the admin list tables) but well about the wp_comments_list_table which has a much narrower target as it only feeds the comments list table. And this is exactly what I need to modify for good. Nov 11, 2012 at 15:49
  • 1
    My point being that WordPress doesn't specifically provide filters/hooks for the WP_List_Table class. The WordPress documentation recommends that use a custom class to extent WP_List_Table rather than try to hack core files. wp_comments_list_table is the class file called by WP_List_Table, so by extension the suggested means to filter your content is to create a custom class that extents the existing core classes.
    – Steve
    Nov 11, 2012 at 16:00
  • Thank you for the suggestion and I know the wp recommendations but my case is specific enough not to be worried about hacking the core files or creating a custom class. As a non hard core developer, I'm much more confident in hacking files than in creating classes from scratch and my website is only meant to live 48 hours so hacking is not an issue. Time is an issue though. So if you have an elegant solution for me, I'm all ears. But suggesting me to create a theoretical custom class will not make it. It only brings me back to square one. Sad but true. Nov 11, 2012 at 16:53

1 Answer 1

5

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 );
9
  • Thank you very much TheDeadMedic. I believe this is supposed to go into the functions.php file and that I have to change 'foobar' for my own custom post_type name. If that's all I have to do, it just doesn't work. The comments related to my custom post type are still shown among the other ones. Nov 11, 2012 at 16:56
  • Well it works for me - which version of WordPress are you running? Nov 11, 2012 at 17:01
  • 3.4.2 (I copy/pasted your entire code in functions.php, renamed foobar accordingly and saved the file. I'm sure about the post type name and there are comments listed on the comments list table for every post type I got so, did I forget something?). Nov 11, 2012 at 17:06
  • edit-comments ensures the filter only runs on the "Manage Comments" area in the admin, and won't affect WP_Comment_Query when it's used elsewhere - just to be clear, we are talking about /wp-admin/edit-comments.php? Nov 11, 2012 at 17:12
  • It worked. The issue was that you created a condition for the edit-comment screen only but I needed it for the wp_comments_list_table. I deleted the condition and now it's ok.Thank you very much. One question if you don't mind: now, if I want to create an admin section with a comments_list table ONLY for the excluded post type, how would I do that? (all I need is a guidance to achieve this, not the whole thing as I believe it's complicated). Thank you once again. Nov 11, 2012 at 17:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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