Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I've got some code like this:

$query_args = array();
$query_args['fields'] = array( 'ID', 'display_name' );
$query_args['role'] = 'subscriber';
$users = get_users( $query_args );
foreach ($users as $user) $users_array[$user->ID] = $user->display_name;

I want to get more roles and also include contributor, author and some custom roles I created with the Role Scoper plugin e.g. Manager, etc. Any ideas how I can do this with get_users?

Thanks

share|improve this question
add comment

6 Answers

The problem with using array_merge is that you can't use pagination. I really like @Andy Adams's solution, but if you're searching on many roles, using his meta query will result in a very slow query (internally it does a new INNER JOIN for each meta query).

My solution is to use a regular expression meta query:

<?php

global $wpdb;
$blog_id = get_current_blog_id();

$roles = array('editor', 'administrator');
$meta_query = array(
    'key' => $wpdb->get_blog_prefix($blog_id) . 'capabilities',
    'value' => '"(' . implode('|', array_map('preg_quote', $roles)) . ')"',
    'compare' => 'REGEXP'
);
$user_query = new WP_User_Query(array(
    'meta_query' = array($meta_query)
));

?>

This generates a query that looks something like:

array(
    'meta_query' => array(
        array(
            'key' => 'wp_capabilities'
            'value' => '"(editor|administrator)"'
            'compare' => 'REGEXP'
        )
    )
);
share|improve this answer
add comment

You could simply merge more user query results. Let's say you want to include both Author and Editor roles. Define the query for each case then use array_merge to consolidate into a single array.

$xuser_query = new WP_user_query(array( 'role' => 'Author'));    
$yuser_query = new WP_user_query(array( 'role' => 'Editor'));
$mergedRoles=array_merge($xuser_query->results,$xuser_query->results);

// further on you might want to do some sorting 
// of the resulting array of objects before looping through it:

if (!empty( $mergedRoles->results ) ) {
 foreach ( $mergedRoles->results as $user ) { 
        echo $user->display_name;
      }
 else echo "nada, no users found";
share|improve this answer
add comment
$users = array_merge( get_users('role=subscriber'), get_users('role=contributor') );

Since get_users() returns an array of users that match the query string given as a param. Just run the get_users() query for every role you want separately and merge the results. You can then loop through $users the same way you would have otherwise.

share|improve this answer
1  
Can you please provide more information explaining why this solves the problem? –  GhostToast Aug 29 '13 at 18:33
    
I edited the original answer to better explain why it works. –  ksloan Aug 31 '13 at 5:34
add comment

You can also do this via a single call to get_users or using a single WP_User_Query by making use of the meta_query argument:

global $wpdb;
$blog_id = get_current_blog_id();

$user_query = new WP_User_Query( array(
    'meta_query' => array(
        'relation' => 'OR',
        array(
            'key' => $wpdb->get_blog_prefix( $blog_id ) . 'capabilities',
            'value' => 'role_one',
            'compare' => 'like'
        ),
        array(
            'key' => $wpdb->get_blog_prefix( $blog_id ) . 'capabilities',
            'value' => 'role_two',
            'compare' => 'like'
        )
    )
) );

The meta_query is pulled from how WP_User_Query handles the role parameter, if you're interested.

share|improve this answer
    
Works like a charm! Thanks for sharing. –  Rilwis Mar 13 '13 at 12:17
    
@Andy-Adams Thanks for sharing. This only seems to display all the roles for me. Is this still working for you? –  helgatheviking Jul 25 '13 at 16:41
    
@helgatheviking What roles are you comparing, specifically? It could depend on what the role names are. –  Andy Adams Jul 25 '13 at 18:32
    
@AndyAdams I'm trying to work this into my Simple User Listing plugin, so I'm trying to make it work for any combination of roles. On my local site, I've tried administrator and editor, but looping through the results all the other roles are still included. –  helgatheviking Jul 25 '13 at 20:03
    
I might recommend using the Debug Queries plugin to see what SQL query is being generated. A lot of times this helps me figure out why I'm getting unexpected results. wordpress.org/plugins/debug-queries –  Andy Adams Jul 26 '13 at 1:06
add comment

I managed to solve this by using this function:

function get_clients() { 

    $users = array();
    $roles = array('subscriber', 'custom_role1', 'custom_role2');

    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);
    endforeach;

    return $users;
}

Then in my theme I can do this:

$users_array = get_clients();
share|improve this answer
    
This is the best option I have come across. Thanks. –  Jake Oct 12 '12 at 18:44
    
@blogjunkie Curious, but how well does this sort? –  helgatheviking Jul 25 '13 at 15:28
add comment

All the parameters from the function get_users are optional. If you specify nothing you will get an array that contains objects corresponding to each and every user of the current blog, including ones with custom roles.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.