Site icon Hip-Hop Website Design and Development

How to query users to count all with a custom capability and limit it to a set of roles?

On a site with 12 custom user roles I need to count users with the ‘supervisor’ custom capability but limited to four specific roles.

The following code counts all users with the ‘supervisor’ capability, but does not limit it to the specific roles:

$args = array(
    'role'           => 'supervisor', //this is a custom capability
    'role__in' =>  array( 'level1', 'level2','level3','level4' ) //these are custom roles
    
);

$user_query = new WP_User_Query( $args );

$supervisors = count($user_query->results);