I'm trying to select only users that are registered for a specific site on my multisite set up and see that the table for the users has collected everyone that is on the network. Is there away I can get only the users on a specific site? Or only select the users from that specific site?
1 Answer
Yes, all the WordPress Multisite users go into the {$wpdb->prefix}users
table; that way they form a pool of users that can each be given access to one or more sites in your network.
You can get the users for a given site by using get_users()
, which can take as an argument the ID of a given site:
// get users for site example.com/my-site
$args = array(
'blog_id' = get_id_from_blogname( 'my-site' ),
);
$users_for_site = get_users( $args );
If you're using Multisite, and the blog_id
argument isn't set, you'll get the users for the current site.
Comparing email addresses
By default, get_users()
returns an array of WP_User
objects, so you should be able to compare your list of email addresses (assumed to be contained in the array $my_user_emails
in the code below) to the list of addresses returned by get_users()
:
// uses $users_for_site from the previous code block
$no_email_entered = array();
foreach( $users_for_site as $user ) {
if( ! in_array( $user->user_email, $my_user_emails ) ) {
$no_email_entered[] = $user->user_email;
}
}
// $no_email_entered should now contain all the email addresses
// in the list of site users that are *not* in your table
References
-
Thanks, how could I compare this information to the data in a separate table? I want to see how many registered users did not input their email into a form I created and made a table for. Commented Aug 29, 2014 at 16:57
-
I've updated my answer to cover one way to do this. There are probably more elegant ways to do it, but this should work.– Pat JCommented Aug 29, 2014 at 18:07