get_blogs_of_user( int $user_id, bool $all = false ): object[]
- Since
- 3.0.0, 4.7.0
- Source
wp-includes/user.php:1034
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
- Scaling
- Scales with input
- Instructions
- 8–70
- Plugin surface
- 2 hooks
- Called by
- 13
Reads stored settings via get_option(), cached per request but not free on a cold cache.
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 160.
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.
13 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
apply_filters()called directly - optionoption read or write
get_option()called directly - cacheobject cache
wp_cache_get()one call below get_blogs_of_user() - serializeserialisation
maybe_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.
| When | Instructions | Calls it makes |
|---|---|---|
empty($user_id) | 8 | none |
!empty($user_id) && $sites !== null | 17 | apply_filters() |
!empty($user_id) && $sites === null && empty($keys) | 23 | apply_filters(), get_user_meta() |
!empty($user_id) && $sites === null && !empty($keys) && is_multisite() && empty($site_ids) | 46–53 | apply_filters(), get_user_meta(), is_multisite(), array_keys(), apply_filters() |
!empty($user_id) && $sites === null && !empty($keys) && is_multisite() && !empty($site_ids) | 56–70 | apply_filters(), get_user_meta(), is_multisite(), array_keys(), get_sites(), apply_filters() |
!empty($user_id) && $sites === null && !empty($keys) && !is_multisite() | 65 | apply_filters(), get_user_meta(), is_multisite(), get_current_blog_id(), get_option(), get_option() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 160 | 8–70 | 16 | |
| 8.5 | 160 | 8–70 | 16 | |
| 8.4 | 160 | 8–70 | 16 | 8 fewer instructions than PHP 8.3 |
| 8.3 | 168 | 8–70 | 16 | |
| 8.2 | 168 | 8–70 | 16 | |
| 8.1 | 168 | 8–70 | 16 | 1 fewer instruction than PHP 7.4 |
| 7.4 | 169 | 8–70 | 16 |
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:
- apply_filters( pre_get_blogs_of_user )filterline 1057 (+23 into the body)
Filters the list of a user's sites before it is populated.
- apply_filters( get_blogs_of_user )filterline 1148 (+114 into the body)
Filters the list of sites a user belongs to.
Uses · 9
- apply_filters()Calls the callback functions that have been added to a filter hook.
- get_user_meta()Retrieves user meta field for a user.
- is_multisite()Determines whether Multisite is enabled.
- get_current_blog_id()Retrieves the current site ID.
- get_option()Retrieves an option value based on an option name.
- str_ends_with()Polyfill for `str_ends_with()` function added in PHP 8.0.
- str_starts_with()Polyfill for `str_starts_with()` function added in PHP 8.0.
- get_sites()Retrieves a list of sites matching requested arguments.
- stdClass::__construct()
Used by · 13
- WP_Admin_Bar::initialize()Initializes the admin bar.
- WP_MS_Users_List_Table::column_blogs()Handles the sites column output.
- _access_denied_splash()Displays an access denied message when a user tries to view a site's dashboard they do not have access to.
- choose_primary_blog()Handles the display of choosing a user's primary site.
- confirm_delete_users()
- get_active_blog_for_user()Gets one of a user's active blogs.
- get_dashboard_url()Retrieves the URL to the user's dashboard.
- get_most_recent_post_of_user()Gets a user's most recent post.
- remove_user_from_blog()Removes a user from a blog.
- signup_another_blog()Shows a form for returning users to sign up for another site.
- wp_dashboard_quick_press()Displays the Quick Draft widget.
- wp_xmlrpc_server::wp_getUsersBlogs()Retrieves the blogs of the user.
Show all 13
- wpmu_delete_user()Deletes a user and all of their posts from the network.
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.
Signature, return type and hooks compared across 5 parsed releases.
get_sites().from the docblockAbout this page
- Parsed data
- Generated from the wordpress-develop 7.0.4 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.