Is there a way to list only the users that has a specific capability, such us "publish_posts" ?
If you pass a user ID to it, you will get that user's capabilities. If you don't pass anything to it, you'll get the current user's capabilities instead. It will return an array of capabilities like the one pictured below, or an empty array if the user has no capabilities or doesn't exist.
WordPress stores the Roles and their Capabilities in the options table under the user_roles key.
Author. Users with the Author role have complete control over their content, they may add, edit, publish and delete their own posts and upload images. They can also edit and delete their WordPress profile. Authors have no access to content produced by other users.
You can just retrieve all users. Then loop through them in a foreach. Check if the user has a specific capability then push the users to another array and use that array to list them.
$all_users = get_users();
$specific_users = array();
foreach($all_users as $user){
if($user->has_cap('specific_capability')){
$specific_users[] = $user;
}
}
NOTE: It seemed a nice quick and dirty solution at the time, but now I would recommend writing a query. I do not have the time to investigate this for you, so if the one downvoting this would be so kind to answer this question instead of downvoting an answer which was an actual help to the inquirer, that would be nice.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With