Recently, I needed to get all users of a certain role to modify a Comments Widget to remove those. Since WordPress 3.1, WordPress has made this quite easy to do now.
[php]
function wps_get_users_of_role( $role ) {
$wp_user_search = new WP_User_Query( array( 'role' => $role ) );
return $wp_user_search->get_results();
}
[/php]
This will give you an array that contains the following in an object:
[php]<pre>stdClass Object
(
[ID] => 1
[user_login] => username
[user_pass] => serialized PASSWORD
[user_nicename] => mynicename
[user_email] => [email protected]
[user_url] => http://mydomain.com
[user_registered] => GMT Time Stamp
[user_activation_key] => Something funky!
[user_status] => 0
[display_name] => My Display Name
)</pre>
[/php]
You can even set a predefined array and get only necessary information.
[php]
$args = array();
$args[0] = 'user_login';
$args[1] = 'user_nicename';
$args[2] = 'user_email';
$args[3] = 'user_url';
$wp_user_search = new WP_User_Query( array( 'role' => 'editor', 'fields' => $args ) );
$editors = $wp_user_search->get_results();
[/php]
Here's the result:
[php]<pre>stdClass Object
(
[user_login] => wpsmith
[user_nicename] => wpsmith
[user_email] => [email protected]
[user_url] =>
)</pre>
[/php]
kaito says
Hi it’s nice u have such solution but for me it shows only 50 resutl and i have over 100 users on site with secyfic role how to show more then 50 rows?
Travis Smith says
Hello Kaito,
You will need to append the code with the following:
[php]
$editors->set_pagination_args( array(
‘total_items’ => $editors->get_total(),
‘per_page’ => 100,
) );
[/php]
kaito says
I have lithel diverend
[code]
function s8_wpcfs_get_users_with_role($role)
{
$wp_user_search = new WP_User_Search($usersearch, $userspage, $role );
return $wp_user_search->get_results();
}
[/code]
and than i use
[code]
$editors = s8_wpcfs_get_users_with_role(‘file_sharer’);
if ($editors)
{echo ‘<table class="s8-wpcfs-table">’;
foreach($editors as $editor )
{
$s8_wpcfs_main_page_id = get_option(‘WP_Client_File_Share’);
$user_info = get_userdata($editor);
$user_page_title = $user_info->user_login . ‘ prywatna strona Upload’;
$page = get_page_by_title($user_page_title);
echo ‘<tr>’;
echo ‘<td>Pliki wysłane przez <strong>’ . $user_info->user_login . ‘</strong> – </td>’;
echo ‘<td><a href="’ . $page->guid . ‘">zobacz wysłane pliki</a></td>’;
echo ‘<td><a href="mailto:’ . $user_info->user_email . ‘">Wyślij maila na ‘ . $user_info->user_email . ‘</td>’;
}
echo ‘</tr>’;
echo ‘</table>’;
}
[/code]
and this set me up with 50 rows…
kaito says
Ok sory for spamming it was missing
[code]
$wp_user_search = new WP_User_Query( array( ‘role’ => ‘file_sharer’, ‘fields’ => $user_info ) );
$editors = $wp_user_search->get_results();
[/code]