Using the comments_pre_query
filter, this seemed to work well in development and testing:
/**
* @param null $comment_data Return an array of comment data to short-circuit WP's comment query.
* @param WP_Comment_Query $query WP_Comment_Query instance, passed by reference.
*/
add_filter( 'comments_pre_query', static function ( $comment_data, $query ) {
// Limit to admin area.
if ( ! is_admin() ) {
return null;
}
// Exclude administrators.
if ( current_user_can( 'manage_options' ) ) {
return null;
}
// Don't override existing limit to specific author(s).
if ( ! empty( $query->query_vars['post_author__in'] ) ) {
return null;
}
// Include definition for function if missing.
if ( ! function_exists( 'get_current_screen' ) ) {
require_once 'wp-admin/includes/screen.php';
}
$screen = get_current_screen();
// If screen has not yet been defined, do a basic check.
if ( is_null( $screen ) && str_contains( $_SERVER['PHP_SELF'], 'edit-comments.php' ) ) {
$screen = (object) array(
'id' => 'edit-comments',
);
}
// If screen not identified, bail.
if ( empty( $screen ) || ! is_object( $screen ) ) {
return null;
}
// If incorrect screen, bail.
if ( 'edit-comments' !== $screen->id ) {
return null;
}
// Limit comments to posts authored by current user.
$query->query_vars['post_author__in'] = array( get_current_user_id() );
return null;
}, 10, 2 );