List All Users in WordPress

The possibility in WordPress to display all users is not available in a standard installation. To get a simple list of all users, you can use following function. To display just specific user groups or users, there are many template tags available to do so, go to WordPress-Codex – Author Tags.

it’s a simple, not optimized database query. It should be just a basic example and can or must be extend furthermore. Notice, „Registered Readers” are not saved via user_level in the database. This made it necessary to have a separate database query.

You have the possibility to give the function two parameters. To display only one user group you can use the parameter $userlevel. If you need to display first name and last name, then the parameter $show_fullname has to set to TRUE, otherwise it will display the user name only.

To use the function, you have to put it in your functions.php of your theme. You can call the function with the help of the PHP Plugin (z.B. Exec-PHP) or in your template.

Only administrator, only user name
All user, first and last name
Only reader, first and last name

The function for functions.php or directly in your template.

function fb_list_authors($userlevel = 'all', $show_fullname = true) {
	global $wpdb;
	
/*
 all = Display all user
 1 = subscriber
 2 = editor
 3 = author
 7 = publisher
10 = administrator
*/

if ( $userlevel == 'all' ) {
	$author_subscriper = $wpdb->get_results("SELECT * from $wpdb->usermeta WHERE meta_key = 'wp_capabilities' AND meta_value = 'a:1:{s:10:\"subscriber\";b:1;}'");
	foreach ( (array) $author_subscriper as $author ) {
		$author    = get_userdata( $author->user_id );
		$userlevel = $author->wp2_user_level;
		$name      = $author->nickname;
		if ( $show_fullname && ($author->first_name != '' && $author->last_name != '') ) {
			$name = "$author->first_name $author->last_name";
		}
		$link = '
  • ' . $name . '
  • '; echo $link; } $i = 0; while ( $i <= 10 ) { $userlevel = $i; $authors = $wpdb->get_results("SELECT * from $wpdb->usermeta WHERE meta_key = 'wp_user_level' AND meta_value = '$userlevel'"); foreach ( (array) $authors as $author ) { $author = get_userdata( $author->user_id ); $userlevel = $author->wp2_user_level; $name = $author->nickname; if ( $show_fullname && ($author->first_name != '' && $author->last_name != '') ) { $name = "$author->first_name $author->last_name"; } $link = '
  • ' . $name . '
  • '; echo $link; } $i++; } } else { if ($userlevel == 1) { $authors = $wpdb->get_results("SELECT * from $wpdb->usermeta WHERE meta_key = 'wp_capabilities' AND meta_value = 'a:1:{s:10:\"subscriber\";b:1;}'"); } else { $authors = $wpdb->get_results("SELECT * from $wpdb->usermeta WHERE meta_value = '$userlevel'"); } foreach ( (array) $authors as $author ) { $author = get_userdata( $author->user_id ); $userlevel = $author->wp2_user_level; $name = $author->nickname; if ( $show_fullname && ($author->first_name != '' && $author->last_name != '') ) { $name = "$author->first_name $author->last_name"; } $link = '
  • ' . $userlevelname[$userlevel] . '
  • '; $link .= '
  • ' . $name . '
  • '; echo $link; } } }

    Alternatively you can use a very small syntax. You must customize the fields of the query.

    get_results("SELECT ID, display_name FROM $wpdb->users ORDER BY ID");
    
    foreach ( $wp_user_search as $userid ) {
    	$user_id       = (int) $userid->ID;
    	$user_login    = stripslashes($userid->user_login);
    	$display_name  = stripslashes($userid->display_name);
    
    	$return  = '';
    	$return .= "\t" . '
  • '. $display_name .'
  • ' . "\n"; print($return); } ?>

    Posted

    in

    by

    Comments

    8 responses to “List All Users in WordPress”

    1. Just a User Avatar
      Just a User

      User levels are really overrated.. and are completely ignored by WP now.

      Query for the roles directly.. However it is you’re supposed to query roles..

    2. Frank Avatar

      right. WP had include the roles and have secure the user_level. You can use the user_level and roles for different asks in code.

    3. Will Norris Avatar

      I know this post is a little older, but thought I’d chime in… why not just use get_users_of_blog(). That gets all the user information you’d need, and you could simply write an array_filter() callback function to filter to a specific user role.

    4. Brandon Avatar
      Brandon

      Is there a way to only publish some users? I have users across the permission span whose information I would like to dynamically display on a staff page. I will from time to time allow a guest blogger access for a period but I don’t want their bio on this page. I guess I’m asking if there is a way to “group” users through a custom field or something of that nature.

      Worst case scenario, I canjust create a page which lists posts on each person from the “staff” category or something, but then the bio information that page and and in their user record will be potentially out of sync.

    5. Michael Avatar

      Brandon, you can change the syntax from second example like

      $wp_user_search = $wpdb->get_results("SELECT ID, display_name FROM $wpdb->users WHERE ID IN(1,2,3,4,5) ORDER BY ID");
      

      IN(1,2,3,4,5) => thats the user ids to be listed.

    6. anmari Avatar

      For the non coders, the following user list plugin may be useful to achieve various user queries and lists – recently launched for http://weblogtoolscollection.com/pluginblog/2009/07/31/amr-users-plugin-for-user-lists-and-reporting/.

      It is aimed at being as configurable and generally useful as possible, so needs to be tested against various peoples different uses of the user data.

      I’d be interested in feedback and feature ideas. I used it when doing a website using registerplus for extra user fields and subscribe2 and “your members” plugins and needing to get filtered and unified listings out,

      It is of course also available at wordpress: http://wordpress.org/extend/plugins/amr-users/

    7. Drew Avatar

      Hey Guys,
      I’m listing all users and and it’s working great, but how do I exclude all users who haven’t posted anything? This is what I have so far:

      $authors = $wpdb->get_results(“SELECT ID, user_nicename from $wpdb->users WHERE ID != ‘1’ ORDER BY rand()”);

      Thanks for the help!

    8. Joshua Avatar

      Hey,

      I’m trying to get a slick way of ordering by lastname. I’m also already sorting out all users that are not authors or administrators (to avoid the subscribers and contributors, really). Here’s what I’ve got so far:

      $authors = $wpdb->get_results("SELECT ID, user_nicename from $wpdb->users WHERE display_name 'admin' ORDER BY display_name");

      foreach ($authors as $author ) {
      $role = 'author';
      $role2 = 'administrator';
      $userType = get_user_meta($author->ID, 'wp_capabilities', true);
      if (key($userType) == $role || key($userType) == $role2) {
      ...... code .....
      }

      Now, how do I do this and sort by last name? I can’t use “last_name” or “lastname” instead of display_name, tried that alread. (something to do with users versus user_meta, right?).

      Thanks!
      Joshua