WordPress get_users Role Array

Congrats, You are Subscribed to Receive Updates.

WordPress get_users Role Array

WordPress get_users Role Array. Yes, we may have a situation to get the users of two roles.  But in default WordPress function does not support array of roles to get its users. The default function supports to provide a string , ( a role in text). it wont allow us to pass an array of data’s. So lets do it in custom way.  Here i have few choices, which i tired it, but  i usually prefers the first one for the best results.

$kv_roles = array('author','editor');
$users_list = array();
foreach ($kv_roles as $single_role) {
    $args = array('role'=>$single_role);
    $users_based_on_role = get_users($args);
    $users_list = array_merge($users_based_on_role ,$users_list );
echo "<ul> " ; 
foreach ($users_list as $single_user) {
    echo '<li>' . $user->user_email . '</li>';
echo "</ul> ";

The above snippet helps you to get the author and editor users.  i created a list with it.    There is an advantage in it.  not only two user roles, you can get N number of user roles from it.  Let’s see the next one.

function kv_get_users_list() { 

    $users = array();
    $roles = array('subscriber', 'author', 'editor');

    foreach ($roles as $role) :
        $users_query = new WP_User_Query( array( 
            'fields' => 'all_with_meta', 
            'role' => $role, 
            'orderby' => 'display_name'
            ) );
        $results = $users_query->get_results();
        if ($results) $users = array_merge($users, $results);

    return $users;

$users_list =  kv_get_users_list();

This is almost similar to the previous snippet. but here we are using WP_User_Query class directly.  The first one  we used get_users function for the better results. If you prefer very simple method to get two  role users list.  lets use the below one. Very short and simple to use.

$first_role_users = get_users('role=author'); //First role list

$second_role_users = get_users('role=editor'); // Second roles List

$users_list = array_merge($first_role_users  , $second_role_users ); //Merge fucntion

The same way, we can use the WP_User_Query for it.  Let’s try it.

$authors= new WP_user_query(array( 'role' => 'Author'));    

$editors= new WP_user_query(array( 'role' => 'Editor'));


These are all the different ways to get the users from multiple array of user roles.


About Varadharaj V

The founder of Kvcodes, Varadharaj V is an ERP Analyst and a Web developer specializing in WordPress(WP), WP Theme development, WP Plugin development, Frontaccounting(FA), Sales, Purchases, Inventory, Ledgers, Payroll & HRM, CRM, FA Core Customization, PHP and Data Analyst. Database Management Advance Level

Comment Below

Your email address will not be published. Required fields are marked *


Current ye@r *



Subscribe E-mail Updates

Congrats, You are Subscribed to Receive Updates.


Category Posts