开发者

How WordPress can list users with specific capabilities

开发者 https://www.devze.com 2023-04-11 20:10 出处:网络
Is there a way to list only the users that has a开发者_如何转开发 specific capability, such us \"publish_posts\" ?To select users with certain capabilities you can use WP_User_Query with meta_query pa

Is there a way to list only the users that has a开发者_如何转开发 specific capability, such us "publish_posts" ?


To select users with certain capabilities you can use WP_User_Query with meta_query parameter, because WP stores capabilities as a serialized string in user_meta table. Also remember that due to availability to have multisite installation capabilities name in user meta looks like wp_table_prefix_capabilities.

global $wpdb;
// meta-key name
$capabilities_field_name=$wpdb->prefix.'capabilities';
//array as argument for our query
$qargs=[
     'role' => ['Customer'], // use this if you need to query by role at the same time
     'meta_query'=>
         [
            'relation' => 'OR', // optional if you'll need to select more than
                                //  one capability just add this and create same array
                               // as down below describing what are you looking for 
             [
                'key' => $capabilities_field_name,
                'value' => 'your_role_name',
                'compare' => 'LIKE',
             ],

       // here could be same array [key,value,compare]... as above with another capability      
       // but you'll need to add extra argument showing relationship between them see above 'relation parameter'

         ],
     'number'=> -1 // to select all users
 ];

$usersQuery=new WP_User_Query($qargs); // instantiate UserQuery with $qargs

$users=$usersQuery->get_results(); // get all results as array of WPUser objects

Hope it helps somebody:) Note [vars] could be substituted to array(vars), I like [] short syntax but it's supported only since php 5.4.


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.


You can list users with WP_User_Query, but afaik you can only return different roles, not permissions, maybe that's already what you want! There's also a site where you can see the different roles in the wordpress documentation.


You will first need to get all the roles that contain that capability. Then you can search users based on the roles that contain that capability.

$roles = array();
foreach ( wp_roles()->roles as $role_name => $role_obj ) {
    if ( ! empty( $role_obj['capabilities']['my_capability_name'] ) ) {
        $roles[] = $role_name;
    }
}

$users = get_users( array( 'role__in' => $roles ) );
  • This does not account for if another role has "Deny" on that capability and your users can contain multiple roles. If so then you will also need to add a "user_can()" condition when looping through your Users. https://developer.wordpress.org/reference/functions/user_can/
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号