Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How WordPress can list users with specific capabilities

Tags:

wordpress

Is there a way to list only the users that has a specific capability, such us "publish_posts" ?

like image 637
KodeFor.Me Avatar asked Oct 08 '11 08:10

KodeFor.Me


People also ask

How do I get user capabilities on WordPress?

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.

Where is user role stored WordPress?

WordPress stores the Roles and their Capabilities in the options table under the user_roles key.

What user role would you assign to someone so they can write and publish only their posts and no one else's?

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.


1 Answers

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.

like image 199
Ogier Schelvis Avatar answered Nov 29 '22 03:11

Ogier Schelvis