Get all user list ans email address in WordPress using WP_User_Query() and get_users()
In WordPress site you may need to get list of all user for many reason. there are many possible solutions are available by which you can access list of all resisted user to your site.
Here i am providing the best possible solutions to get list of all registered user.
Solution 1 : Get all user using WordPress WP_User_Query() function.
WP_User_Query() method was include in WordPress 3.1 version. It allows querying the WordPress User database trough it’s available methods. It provide the easiest and secure way to access user from database.
Step 1. minimal code snippets for getting all user email address in WordPress
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<?php // WordPress get all user emails $args = array('orderby' => 'display_name'); $wp_user_query = new WP_User_Query($args); $authors = $wp_user_query->get_results(); if (!empty($authors)) { echo '<ul>'; foreach ($authors as $author) { $author_info = get_userdata($author->ID); echo '<li>' . $author_info->user_email . '</li>'; } echo '</ul>'; } else { echo 'No results'; } ?> |
Step 2. Explained and details code snippets for getting all user name and email in WordPress
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
// prepare arguments $args = array( // search only for Authors role 'role' => 'Author', // order results by display_name 'orderby' => 'display_name', // check for two meta_values 'meta_query' => array( array( // uses compare like WP_Query 'key' => 'some_user_meta_key', 'value' => 'some user meta value', 'compare' => '>' ), array( // by default compare is '=' 'key' => 'some_other_user_meta_key', 'value' => 'some other meta value', ), // add more )); // Create the WP_User_Query object $wp_user_query = new WP_User_Query($args); // Get the results $authors = $wp_user_query->get_results(); // Check for results if (!empty($authors)) { echo '<ul>'; // loop trough each author foreach ($authors as $author) { // get all the user's data $author_info = get_userdata($author->ID); echo '<li>'.$author_info->first_name.' '.$author_info->last_name.'</li>'; } echo '</ul>'; } else { echo 'No authors found'; } |
Solution 2: Get all user name and email address using WordPress get_users() function.
You can get all users of WordPress using following code. These code print all WordPress user’s list.
This code snippets uses the get_users() method to get all user information and data.
1 2 3 4 5 6 7 8 9 |
<?php $all_users = get_users(); echo '<ol>'; foreach ($all_users as $user) { echo '<li><span>' . esc_html($user->user_email) . ' : ' . esc_html($user->display_name) . '</span></li>'; } echo '</ol>'; ?> |
Solution 3: Get Users List by Role in WordPress
This are the WordPress per-defined user roles, you can fetch a specific types user also.
- Super Admin
- Administrator
- Editor
- Author
- Subscriber
- Contributor
Display all user of subscribers role:
This code snippets will fetch all subscribers user similarly you can get any user role specific list by changing “role” perimeter value in code .
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<?php $args1 = array( 'role' => 'subscriber', 'orderby' => 'user_nicename', 'order' => 'ASC' ); $subscribers = get_users($args1); echo '<ul>'; foreach ($subscribers as $user) { echo '<li>' . $user->display_name.'['.$user->user_email . ']</li>'; } echo '</ul>'; ?> |
Display all authors list of WordPress
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<?php $args2 = array( 'role' => 'author', 'orderby' => 'user_nicename', 'order' => 'ASC' ); $authors = get_users($args2); echo '<ul>'; foreach ($authors as $user) { echo '<li>' . $user->display_name.'['.$user->user_email . ']</li>'; } echo '</ul>'; ?> |
[paypal-donation]
I’m new to WP and found your site. I love it.
With the above code (Get all users list and email address in WordPress), what file do I insert it within? functions.php?
Thanks for all the work you’ve done on this site,
Scott
Hi Scott,
Glade to see you here, You have to place this code in that file where you want to fetch or display User List with email ID, You can use this code for feting and mailing new updates on your site. The code snippets is only for “Get all users list and email address in WordPress”, Now it’s depends on your need how you process that data.
Hi!
Thank you for sharing with us this great post, i like it! well done.
My question is how can i insert registration form with custon data like biography, phone, city, comapny and many more to data table use_meta
Nice code …..