Does WordPress get_users support arrays?

Does anyone know if the function get_users supports an array of user roles? My guess is that it doesn’t.

I have the following which works:

Read More
$args =array(
   'role' => 'line_manager'
);
$blogusers = get_users($args);
foreach ($blogusers as $user) { 

I then adapt the above to look like the following and it returns empty.

$args =array(
    'role' => array('line_manager','administrator')
);
$blogusers = get_users($args);
foreach ($blogusers as $user) {

If it doesn’t support arrays, is there a workaround other than doing a wp_query?

Related posts

Leave a Reply

2 comments

  1. Yes, you’re right, it doesn’t support arrays.
    That’s a string to be used in the SQL query LIKE '%"line_manager"%'.

    But a simple array_merge can solve this:

    $argsa =array( 'role' => 'administrator' );
    $a = get_users( $argsa );
    
    $argsb =array( 'role' => 'line_manager' );
    $b = get_users( $argsb );
    
    $users = array_merge( $a, $b );
    

    You could also use the action hook pre_user_query, but it seems overkill.

  2. As @brasofilo said you essentially need to build separate queries and combine the results. Here’s an alternative and slightly shorter {example} way to do it -assuming you want to just collect ID’s:

    $custom_ids = implode(',',get_users('role=administrator&field=ID')); 
    $custom_ids .= ",".implode(',',get_users('role=line_manager&field=ID'));