In following to my comments on the question the follow examples for a solution.
Default Function
WordPress have the function get_users
to get all users for each site, works on single or multisite area. But to get all users from each site in Multisite network is the switch to each site important. The follow example demonstrate this.
// Multisite
// get sites in the Multisite network
$sites = wp_get_sites();
// Switch in each Site
foreach( $sites as $site ) {
switch_to_blog( $site[ 'blog_id' ] );
// Get Users of each site
var_dump( count( get_users() ) );
restore_current_blog();
}
The code above get count (count()
) all users in each site of the network.
If you are in a single site of the network, then is the function get_users()
enough to get all users of this site. The function use the WordPress global $GLOBALS['blog_id']
to identifier the site on his ID.
Alternative for custom requirements
If the function is not helpful, use the WP_User_Query
for your requirements.
The class give you a lot of possibilities to get user data in each site. Also use the helping hands of switch_to_blog()
to switch on each site of the Multisite.
See the codex for helpful hints, parameters and example source.
Small hint, the function get_users
is a wrapper to use it easy of the WP_User_Query
-class.
Hints for Multisite
Grabbing Data From Another Blog in a Network
Getting data from another blog on the same multisite install can be done. Some people use SQL commands to do this, but this can be slow, and error prone.
Although it’s an inherently expensive operation, you can make use of switch_to_blog and restore_current_blog
to make it easier, while using the standard WordPress APIs.
switch_to_blog( $blog_id );
// Do something
restore_current_blog();
restore_current_blog
undos the last call to switch_to_blog
, but only by one step, the calls are not nestable, so always call restore_current_blog
before calling switch_to_blog
again.
Large Network
Listing blogs in a network is possible, but it’s an expensive thing to do.
On default is the WordPress count on 10.000 sites for a network. If you will change this value, see the source below.
add_filter( 'wp_is_large_network', function( $is_large, $type, $count ) {
if ( ! $is_large )
return $is_large;
// $type can be 'sites' or 'users'
if ( 'sites' !== $type )
return $is_large;
// Default is 10000, we add one 0
return $count > 100000;
}, 10, 3 );
It can be done using the wp_get_sites( $args )
function, available since version 3.7 of WordPress. The function accepts an array of arguments specifying the kind of sites you are looking for.
The limit of this function is on 100 sites. If you network is bigger, then you must change this value via parameters on the function.
Cache
The function wp_get_sites( $args )
is since 3.7 inside the core, but haven’t a cache. If you use this often, transform the result in a cache, like WP_Cache
or Transients.
It is also a good idea to cache the get_users()
result, much faster.