WP Smith

Creating WordPress & Genesis Websites Since 2010

  • Home
  • About
  • Services
  • Blog
  • Contact

Feb 10 2012

How to Get All Users of a Specific Role

WordPress Users
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]

Written by Travis Smith · Categorized: WordPress

StudioPress Premium WordPress Themes     WP Engine Managed WordPress Hosting

What can I do for you!?

Custom Development

We develop plugins by determining both business/functional and technical requirements, following WordPress development best practices, and using agile methodology to ensure you get the best solution.

Consulting

Have questions? Need a reliable developer to consult? Please contact us today!

Customized Theme

We can customize your theme or child theme, or create a child theme for you based on your needs while enhancing the performance of every individual attribute.

Customized Plugin

We can customize your plugins, extend plugins (e.g., Gravity Forms, Jetpack, Soliloquy) based on your needs ensuring security, performance, and positive business impact.

Contact Us

About Travis Smith

As a WordPress enthusiast, developer, and speaker, Travis writes about what he learns in WordPress trying to help other WordPress travelers, beginners and enthusiasts with tutorials, explanations, & demonstrations.

Comments

  1. kaito says

    April 5, 2012 at 10:46 am

    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?

    Reply
    • Travis Smith says

      April 5, 2012 at 11:01 am

      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]

      Reply
      • kaito says

        April 5, 2012 at 11:37 am

        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…

        Reply
        • kaito says

          April 5, 2012 at 4:54 pm

          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]

          Reply

Leave a Reply Cancel reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  • Twitter
  • Facebook
  • LinkedIn
  • Google+
  • RSS

Copyright © 2025 � WP Smith on Genesis on Genesis Framework � WordPress � Log in