将此代码粘贴到functions.php 当前活动主题的文件。你也可以把它变成一个插件文件。它使用“the_comments“筛选以从后端表中删除不需要的注释。
/**
* 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\' );