get_blogs_of_user

Get the sites a user belongs to.

Description

(array) get_blogs_of_user( (int) $user_id, (bool) $all = false ); 

Returns (array)

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

Parameters (2)

0. $user_id (int)
The user id.
1. $all — Optional. (bool) => false
Whether to retrieve all sites, or only sites that are not marked as deleted, archived, or spam.

Usage

  1. if ( !function_exists( 'get_blogs_of_user' ) ) { 
  2. require_once ABSPATH . WPINC . '/user.php'; 
  3.  
  4. // The user id. 
  5. $user_id = -1; 
  6.  
  7. // Whether to retrieve all sites, or only sites that are not 
  8. // marked as deleted, archived, or spam. 
  9. $all = false; 
  10.  
  11. // NOTICE! Understand what this does before running. 
  12. $result = get_blogs_of_user($user_id, $all); 
  13.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/user.php  
  1. function get_blogs_of_user( $user_id, $all = false ) { 
  2. global $wpdb; 
  3.  
  4. $user_id = (int) $user_id; 
  5.  
  6. // Logged out users can't have sites 
  7. if ( empty( $user_id ) ) 
  8. return array(); 
  9.  
  10. /** 
  11. * Filters the list of a user's sites before it is populated. 
  12. * Passing a non-null value to the filter will effectively short circuit 
  13. * get_blogs_of_user(), returning that value instead. 
  14. * @since 4.6.0 
  15. * @param null|array $sites An array of site objects of which the user is a member. 
  16. * @param int $user_id User ID. 
  17. * @param bool $all Whether the returned array should contain all sites, including 
  18. * those marked 'deleted', 'archived', or 'spam'. Default false. 
  19. */ 
  20. $sites = apply_filters( 'pre_get_blogs_of_user', null, $user_id, $all ); 
  21.  
  22. if ( null !== $sites ) { 
  23. return $sites; 
  24.  
  25. $keys = get_user_meta( $user_id ); 
  26. if ( empty( $keys ) ) 
  27. return array(); 
  28.  
  29. if ( ! is_multisite() ) { 
  30. $site_id = get_current_blog_id(); 
  31. $sites = array( $site_id => new stdClass ); 
  32. $sites[ $site_id ]->userblog_id = $site_id; 
  33. $sites[ $site_id ]->blogname = get_option('blogname'); 
  34. $sites[ $site_id ]->domain = ''; 
  35. $sites[ $site_id ]->path = ''; 
  36. $sites[ $site_id ]->site_id = 1; 
  37. $sites[ $site_id ]->siteurl = get_option('siteurl'); 
  38. $sites[ $site_id ]->archived = 0; 
  39. $sites[ $site_id ]->spam = 0; 
  40. $sites[ $site_id ]->deleted = 0; 
  41. return $sites; 
  42.  
  43. $site_ids = array(); 
  44.  
  45. if ( isset( $keys[ $wpdb->base_prefix . 'capabilities' ] ) && defined( 'MULTISITE' ) ) { 
  46. $site_ids[] = 1; 
  47. unset( $keys[ $wpdb->base_prefix . 'capabilities' ] ); 
  48.  
  49. $keys = array_keys( $keys ); 
  50.  
  51. foreach ( $keys as $key ) { 
  52. if ( 'capabilities' !== substr( $key, -12 ) ) 
  53. continue; 
  54. if ( $wpdb->base_prefix && 0 !== strpos( $key, $wpdb->base_prefix ) ) 
  55. continue; 
  56. $site_id = str_replace( array( $wpdb->base_prefix, '_capabilities' ), '', $key ); 
  57. if ( ! is_numeric( $site_id ) ) 
  58. continue; 
  59.  
  60. $site_ids[] = (int) $site_id; 
  61.  
  62. $sites = array(); 
  63.  
  64. if ( ! empty( $site_ids ) ) { 
  65. $args = array( 
  66. 'number' => '',  
  67. 'site__in' => $site_ids,  
  68. ); 
  69. if ( ! $all ) { 
  70. $args['archived'] = 0; 
  71. $args['spam'] = 0; 
  72. $args['deleted'] = 0; 
  73.  
  74. $_sites = get_sites( $args ); 
  75.  
  76. foreach ( $_sites as $site ) { 
  77. $sites[ $site->id ] = (object) array( 
  78. 'userblog_id' => $site->id,  
  79. 'blogname' => $site->blogname,  
  80. 'domain' => $site->domain,  
  81. 'path' => $site->path,  
  82. 'site_id' => $site->network_id,  
  83. 'siteurl' => $site->siteurl,  
  84. 'archived' => $site->archived,  
  85. 'mature' => $site->mature,  
  86. 'spam' => $site->spam,  
  87. 'deleted' => $site->deleted,  
  88. ); 
  89.  
  90. /** 
  91. * Filters the list of sites a user belongs to. 
  92. * @since MU 
  93. * @param array $sites An array of site objects belonging to the user. 
  94. * @param int $user_id User ID. 
  95. * @param bool $all Whether the returned sites array should contain all sites, including 
  96. * those marked 'deleted', 'archived', or 'spam'. Default false. 
  97. */ 
  98. return apply_filters( 'get_blogs_of_user', $sites, $user_id, $all );