To select users with certain capabilities, you can use WP_User_Query with the meta_query parameter, since WP stores the capabilities as a json string in user_meta. Also remember that due to the availability of multi-user installation capabilities, the name in the user thrower 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"', //rolename placed in "" in json it is quoted '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 this helps someone :) Note. [Vars] can be replaced with an array (vars), I like the [] short syntax, but only supported with php 5.4.
source share