Paste this code inside the functions.php file of your currently active theme. You can make it a plugin file too. It uses the “the_comments” filter to remove the unwanted comments from the backend table.
/**
* Filter out comments of other posts than current logged-in user.
*
* @param $all_comments all the comments to be displayed in List_Table
*
* @return mixed filtered out comments.
*/
function filter_comments_by_contributor( $all_comments ) {
// get the current logged in user
$current_user = wp_get_current_user();
if ( 0 == $current_user->ID ) {
// Not logged in.
return $all_comments;
} else {
// Logged in.
// check if the logged-in user is a contributor
if ( in_array( 'contributor', (array) $current_user->roles ) ) {
// check if the user is on wp-admin backend,
$screen = get_current_screen();
if ( ! empty( $screen ) && 'edit-comments' == $screen->id ) {
// get all posts by that contributor
$args = array(
'author' => $current_user->ID,
'posts_per_page' => - 1,
'fields' => 'ids'
);
$contributor_posts = get_posts( $args );
// unset the comments given on posts other than his/her.
foreach ( $all_comments as $key => $value ) {
if ( ! in_array( $value->comment_post_ID, $contributor_posts ) ) {
unset( $all_comments[ $key ] );
}
}
}
return $all_comments;
} else {
return $all_comments;
}
}
}
add_filter( 'the_comments', 'filter_comment_by_contributor' );