This useful little tip will show you how to retrieve an array of registered users from your WordPress database. We will use the get_users() function to query the database and return an array of users matching our parameters.

As defined in the codex, here is the function and all of its accepted parameters:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?php $args = array(
	'blog_id' => $GLOBALS['blog_id'],
	'role' => ,
	'meta_key' => ,
	'meta_value' => ,
	'meta_compare' => ,
	'include' => array(),
	'exclude' => array(),
	'orderby' => 'login',
	'order' => 'ASC',
	'offset' => ,
	'search' => ,
	'number' => ,
	'count_total' => true,
	'fields' => 'all',
	'who' => 
 ); ?>
 
<?php get_users( $args ); ?>

So, for example, if we are running a multi site blog network, then we could do the following to retrieve all of the users from blog ID 2, excluding the admin user, and then store their emails in another array:

1
2
3
4
5
 
$registered_users = get_users(array('blog_id' => 2, 'exclude' => array(1) ));
foreach ($registered_users as $user) {
	$user_emails[] = $user->user_email;
}

[box style=”notice”]This is a great trick to use for email notification plugins, such as Post Notify[/box]

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