wppaste
WordPress

get_blogs_of_user( int $user_id, bool $all = false ): object[]

Since
3.0.0, 4.7.0
Source
wp-includes/user.php:962
Gets the sites a user belongs to.

Compatibility

WordPress
since 4.7.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$user_idint
User ID
$allbooloptional
Whether to retrieve all sites, or only sites that are not marked as deleted, archived, or spam.Default: false

Return value

object[]
A list of the user's sites. An empty array if the user doesn't exist or belongs to no sites.

Performance profile

How much work a call to get_blogs_of_user() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Heavy

Reads stored settings via get_option(), cached per request but not free on a cold cache.

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
8–70

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 160.

Plugin surface
2 hooks

Third-party callbacks on 'pre_get_blogs_of_user', 'get_blogs_of_user' run inside this call, and their cost is not bounded by anything here.

Called by
13

13 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • hookthird-party callbacksapply_filters()called directly
  • optionoption read or writeget_option()called directly
  • cacheobject cachewp_cache_get()one call below get_blogs_of_user()
  • serializeserialisationmaybe_unserialize()one call below get_blogs_of_user()

Further down the call graph this can also reach query and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 6 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost get_blogs_of_user() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
empty($user_id)8none
!empty($user_id) && $sites !== null17apply_filters()
!empty($user_id) && $sites === null && empty($keys)23apply_filters(), get_user_meta()
!empty($user_id) && $sites === null && !empty($keys) && is_multisite() && empty($site_ids)46–53apply_filters(), get_user_meta(), is_multisite(), array_keys(), apply_filters()
!empty($user_id) && $sites === null && !empty($keys) && is_multisite() && !empty($site_ids)56–70apply_filters(), get_user_meta(), is_multisite(), array_keys(), get_sites(), apply_filters()
!empty($user_id) && $sites === null && !empty($keys) && !is_multisite()65apply_filters(), get_user_meta(), is_multisite(), get_current_blog_id(), get_option(), get_option()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev1608–7016
8.51608–7016
8.41608–70168 fewer instructions than PHP 8.3
8.31688–7016
8.21688–7016
8.11688–70161 fewer instruction than PHP 7.4
7.41698–7016

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Hooks and filters fired · 2

2 hooks fire while get_blogs_of_user() runs, in this order:

  1. apply_filters( pre_get_blogs_of_user )filterline 985 (+23 into the body)

    Filters the list of a user's sites before it is populated.

  2. apply_filters( get_blogs_of_user )filterline 1076 (+114 into the body)

    Filters the list of sites a user belongs to.

Uses · 9

Used by · 13

Show all 13

Source code

function get_blogs_of_user( $user_id, $all = false ) {	global $wpdb; 	$user_id = (int) $user_id; 	// Logged out users can't have sites.	if ( empty( $user_id ) ) {		return array();	} 	/**	 * Filters the list of a user's sites before it is populated.	 *	 * Returning a non-null value from the filter will effectively short circuit	 * get_blogs_of_user(), returning that value instead.	 *	 * @since 4.6.0	 *	 * @param null|object[] $sites   An array of site objects of which the user is a member.	 * @param int           $user_id User ID.	 * @param bool          $all     Whether the returned array should contain all sites, including	 *                               those marked 'deleted', 'archived', or 'spam'. Default false.	 */	$sites = apply_filters( 'pre_get_blogs_of_user', null, $user_id, $all ); 	if ( null !== $sites ) {		return $sites;	} 	$keys = get_user_meta( $user_id );	if ( empty( $keys ) ) {		return array();	} 	if ( ! is_multisite() ) {		$site_id                        = get_current_blog_id();		$sites                          = array( $site_id => new stdClass() );		$sites[ $site_id ]->userblog_id = $site_id;		$sites[ $site_id ]->blogname    = get_option( 'blogname' );		$sites[ $site_id ]->domain      = '';		$sites[ $site_id ]->path        = '';		$sites[ $site_id ]->site_id     = 1;		$sites[ $site_id ]->siteurl     = get_option( 'siteurl' );		$sites[ $site_id ]->archived    = 0;		$sites[ $site_id ]->spam        = 0;		$sites[ $site_id ]->deleted     = 0;		return $sites;	} 	$site_ids = array(); 	if ( isset( $keys[ $wpdb->base_prefix . 'capabilities' ] ) && defined( 'MULTISITE' ) ) {		$site_ids[] = 1;		unset( $keys[ $wpdb->base_prefix . 'capabilities' ] );	} 	$keys = array_keys( $keys ); 	foreach ( $keys as $key ) {		if ( ! str_ends_with( $key, 'capabilities' ) ) {			continue;		}		if ( $wpdb->base_prefix && ! str_starts_with( $key, $wpdb->base_prefix ) ) {			continue;		}		$site_id = str_replace( array( $wpdb->base_prefix, '_capabilities' ), '', $key );		if ( ! is_numeric( $site_id ) ) {			continue;		} 		$site_ids[] = (int) $site_id;	} 	$sites = array(); 	if ( ! empty( $site_ids ) ) {		$args = array(			'number'   => '',			'site__in' => $site_ids,		);

Changelog

Introduced in 3.0.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

4.7.0
Converted to use get_sites().from the docblock
3.0.0
Introduced.from the docblock

About this page

Parsed data
Generated from the wordpress-develop 6.7.7 tag, from src/wp-includes/user.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.