Kann ich nur die Kommentare einer bestimmten Benutzerrolle für dieselbe Benutzerrolle anzeigen?
Beispiel: Der angemeldete Benutzer gehört zur Benutzerrolle "Werbetreibende". Er sollte nur die Kommentare sehen, die er und alle Benutzer unter seiner Rolle abgegeben haben.
Vielen Dank.
Ja, du kannst,
nur Frontend
sie müssten jeden Kommentarautor auf seine Rolle überprüfen. Wenn die Rolle dieses Kommentars nicht Adverstisers ist, entfernen Sie ihn aus dem Array der Kommentare, die an diesen Beitrag angehängt sind.
Dann müssten Sie nur die Kommentare zurückgeben, wenn der aktuell angemeldete Benutzer auch aus der Rolle Advertiser stammt.
mit dem comments_array
filter können wir jeden kommentar eines posts durchgehen. Dieser Filter wird jedoch in comments_template angewendet, sodass er sich nicht auf die Zugriffskommentare im Backend auswirkt.
Hier ist, wie Sie es machen können
add_filter( 'comments_array' , 'wpse_filter_by_role_frontend' , 10, 2 );
function wpse_filter_by_role_frontend( $comments, $post_id ){
$current_user = wp_get_current_user(); // retrieve the currently logged in user
// go over each comments for the current post
foreach( $comments as $key => $comment ){
$comment_author = new WP_User( $comment->user_id ); // for each comment get the author user object
// here we say unset the current comment if the role of the comment author is not the same as the role of the logged in user
if( $comment_author->roles[0] != $current_user->roles[0] ){
unset( $comments[$key] );
}
}
// Return the filtered $comments array
return $comments;
}
Frontend und Backend
add_action( 'pre_get_comments' , 'wpse_hide_for_backend' );
function wpse_hide_for_backend( $comments_query ){
// Hide all for non logged in users
if( !is_user_logged_in() ){
return $comments_query->query_vars['comment__in'] = array(0);
}
$current_user = wp_get_current_user();
// if you don't want to apply restrictions to admins
if( $current_user->roles[0] == 'administrator' ){
return $comments_query;
}
$user_ids = get_users( array(
'role__in' => $current_user->roles,
'fields' => 'ID'
) );
$comments_query->query_vars['author__in'] = $user_ids;
}
EDIT
Ich habe die zweite Funktion geändert.
Als ich die Antwort von @ birgire sah, wurde mir klar, dass es ein author_in
-Abfrage-Argument gab. Mit get_users
und dem Abfrage-Argument role__in
können wir den gewünschten Effekt sowohl im Front-End als auch im Back-End erzielen.
Also danke an @birgire für die Inspiration :)
EDIT
Um mehr als nur die Rolle des aktuellen Benutzers beim Abrufen von Kommentaren zuzulassen (z. B. das Hinzufügen von Kommentaren von Administratoren), fügen Sie dem Array in role__in
einfach die gewünschten Rollen hinzu
so würde die funktion werden
add_action( 'pre_get_comments' , 'wpse_hide_for_backend' );
function wpse_hide_for_backend( $comments_query ){
// Hide all for non logged in users
if( !is_user_logged_in() ){
return $comments_query->query_vars['comment__in'] = array(0);
}
$current_user = wp_get_current_user();
// if you don't want to apply restrictions to admins
if( $current_user->roles[0] == 'administrator' ){
return $comments_query;
}
$user_ids = get_users( array(
'role__in' => array(
'administrator',
$current_user->roles[0],
),
'fields' => 'ID'
) );
$comments_query->query_vars['author__in'] = $user_ids;
}
oder (zur besseren Lesbarkeit in diesem Forum)
$permitted_roles = array(
'administrator',
$current_user->roles[0],
);
$user_ids = get_users( array(
'role__in' => $permitted_roles,
'fields' => 'ID'
) );
Hier ist eine Möglichkeit (ungetestet), Kommentare von Benutzern in derselben Rolle wie der aktuelle Benutzer anzuzeigen:
add_filter( 'comments_template_query_args', function( array $args )
{
// Nothing to do for visitors
if( ! is_user_logged_in() )
return $args;
// Nothing to do for threaded comments
if( isset( $args['hierarchical'] ) && 'threaded' === $args['hierarchical'] )
return $args;
// Get current user
$u = wp_get_current_user();
// Nothing to do for users without any roles
if( ! isset( $u->roles ) ||empty( $u->roles ) )
return $args;
// Fetch user ids with the same role
$user_ids = get_users( [ 'role__in' => (array) $u->roles, 'fields' => 'ID' ] );
// Restrict comment authors
if( ! empty( $user_ids ) )
$args['author__in'] = (array) $user_ids;
return $args;
} );
Hier nehmen wir an, dass die Benutzerbasis nicht sehr groß ist, und verwenden den comments_template_query_args
-Filter, um die Hauptkommentarabfrage im comments_template()
-Teil des Themas abzufragen.