2

I have a CPT which supports comments. This CPT is meant for internal use only. However the comments show up in the "Recent comments" default widget. They also show up in the RSS feed.

Is there a simple way to prevent a CPT comments from showing up in widgets, feeds etc.. ?

Thank you

1 Answer 1

1

Removing Custom Post Types' comments from Recent Comments Widget

The arguments for the recent comments widget can be customized using the widget_comments_args filter. To customize the post types types whose comments will be included, we can customize the $post_type variable:

$post_type Post type or array of post types to retrieve affiliated comments for. Pass 'any' to match any value. Default empty.

By default, all post types' comments will be included. The following code sets $post_type to an array containing only post so that only comments from posts will appear. To include additional post types, simply add them to the array.

add_action( 'widget_comments_args', 'wpse_widget_comments_args' );
function wpse_widget_comments_args( $args ) {
    $args['post_type'] = [
        'post',
    ];

    return $args;
}

Removing Custom Post Types' comments from the RSS comments feed

This snippet of code (based on solution found here) will allow you to remove the comments associated with the book and product post types (as an example) from the RSS comment feed (http://example.com/comments/feed). It works by altering the where clause of the comments RSS query using the comment_feed_where filter.

add_filter( 'comment_feed_where', 'wpse_comment_feed_where' );
function wpse_comment_feed_where( $where ) {
    return $where . " AND wp_posts.post_type NOT IN ( 'book', 'product' )";
}

Your Answer

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

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