Display all the registered users from WordPress blog

If you are wondering how to display all the registered users from WordPress blog, then you are on the correct place. There are different ways you can manipulate wp_user query. There are several ways you can query Users, like by role, by email, orders, custom fields, including or excluding posts, returning arguments etc.

Displaying all the users

Say you want to display all the users, use the query given below to list all the users.

<?php 
        // WP_User_Query arguments
$args = array (
	'fields'         => 'all',
);

// The User Query
$user_query = new WP_User_Query( $args );
;

// The User Loop
if ( ! empty( $user_query->results ) ) {
	foreach ( $user_query->results as $user ) {
	 echo '<li>'.$user->display_name.'</li>';
	}
} else {
	echo "No users Found";
}
?>

Displaying all the users of Specific Roles

Say you want to display all the users of specific roles, use the query given below to list all the users. Note default Roles are “Default Roles.”

<?php 
        // WP_User_Query arguments
$args = array (
	'role'           => 'Subscriber',
	'fields'         => 'all',
);

// The User Query
$user_query = new WP_User_Query( $args );
;

// The User Loop
if ( ! empty( $user_query->results ) ) {
	foreach ( $user_query->results as $user ) {
	 echo '<li>'.$user->display_name.'</li>';
	}
} else {
	echo "No users Found";
}
?>

Displaying users including or excluding specific users

Say you want to display all the users of specific roles and exclude or include some users, use the query given below to list all the users. Here, we are including roles with user id 5, 6, 7 and excluding users with roles 56, 66 and 75.

<?php 
        // WP_User_Query arguments
$args = array (
	'role'           => 'Subscriber',
	'include'        => array( 5,7,8 ),
	'exclude'        => array( 56, 66, 75 ),
	'fields'         => 'all',
);

// The User Query
$user_query = new WP_User_Query( $args );
;

// The User Loop
if ( ! empty( $user_query->results ) ) {
	foreach ( $user_query->results as $user ) {
	 echo '<li>'.$user->display_name.'</li>';
	}
} else {
	echo "No users Found";
}
?>

Searching users by display name or email, user url or nickname

If you want to search users by specific display name, the arguments for the WP_USER_QUERY would be as follows: Commas separated database table columns to matches the search keywords. e.g. id, user_login, user_nicename, user_email, user_url

$args = array (
	'role'           => 'Subscriber',
	'search'         => 'dr.code.skm',
	'search_columns' => array( 'user_login' ),
	'fields'         => 'all',
);

If you want to search Users by email, then the query would become like this,

$args = array (
	'role'           => 'Subscriber',
	'search'         => 'itsmeskm99@gmail.com',
	'search_columns' => array( 'user_email' ),
	'fields'         => 'all',
);

Display first 10 users only

If you want only to display first specific number of users, the arguments becomes as follows:

$args = array (
	'role'           => 'Subscriber',
	'number'         => '10',
	'fields'         => 'all',
);

Sorting the users display

If you want only to display first specific number of users, and sort by some orders, then the query would become as follows:

$args = array (
	'role'           => 'Subscriber',
	'number'         => '10',
	'order'          => 'ASC',
	'orderby'        => 'display_name',
	'count_total'    => true,
	'fields'         => 'all',
);
Tags :

About the Author

dr.code.skm

I am backend developer with passion in Web Application development using latest technologies like Laravel, PHP7, React, ECMAScript 6 and WordPress. I prefer spending time analyzing big data with Apache Spark. Apart from that, I do photography.