switch_to_blog

Switch the current blog.

Description

switch_to_blog( (int) $new_blog, (null) $deprecated = null ); 

This function is useful if you need to pull posts, or other information, from other blogs. You can switch back afterwards using restore_current_blog().

Things that aren't switched: - autoloaded options. See #14992 - plugins. See #14941

Parameters (2)

0. $new_blog (int)
The id of the blog you want to switch to. Default: current blog
1. $deprecated — Optional. (null) => null
Deprecated argument

Usage

  1. if ( !function_exists( 'switch_to_blog' ) ) { 
  2. require_once ABSPATH . WPINC . '/ms-blogs.php'; 
  3.  
  4. // The id of the blog you want to switch to. Default: current blog 
  5. $new_blog = -1; 
  6.  
  7. // Deprecated argument 
  8. $deprecated = null; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = switch_to_blog($new_blog, $deprecated); 
  12.  

Defined (1)

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

/wp-includes/ms-blogs.php  
  1. function switch_to_blog( $new_blog, $deprecated = null ) { 
  2. global $wpdb, $wp_roles; 
  3.  
  4. $blog_id = get_current_blog_id(); 
  5. if ( empty( $new_blog ) ) { 
  6. $new_blog = $blog_id; 
  7.  
  8. $GLOBALS['_wp_switched_stack'][] = $blog_id; 
  9.  
  10. /** 
  11. * If we're switching to the same blog id that we're on,  
  12. * set the right vars, do the associated actions, but skip 
  13. * the extra unnecessary work 
  14. */ 
  15. if ( $new_blog == $blog_id ) { 
  16. /** 
  17. * Fires when the blog is switched. 
  18. * @since MU 
  19. * @param int $new_blog New blog ID. 
  20. * @param int $new_blog Blog ID. 
  21. */ 
  22. do_action( 'switch_blog', $new_blog, $new_blog ); 
  23. $GLOBALS['switched'] = true; 
  24. return true; 
  25.  
  26. $wpdb->set_blog_id( $new_blog ); 
  27. $GLOBALS['table_prefix'] = $wpdb->get_blog_prefix(); 
  28. $prev_blog_id = $blog_id; 
  29. $GLOBALS['blog_id'] = $new_blog; 
  30.  
  31. if ( function_exists( 'wp_cache_switch_to_blog' ) ) { 
  32. wp_cache_switch_to_blog( $new_blog ); 
  33. } else { 
  34. global $wp_object_cache; 
  35.  
  36. if ( is_object( $wp_object_cache ) && isset( $wp_object_cache->global_groups ) ) { 
  37. $global_groups = $wp_object_cache->global_groups; 
  38. } else { 
  39. $global_groups = false; 
  40.  
  41. if ( function_exists( 'wp_cache_add_global_groups' ) ) { 
  42. if ( is_array( $global_groups ) ) { 
  43. wp_cache_add_global_groups( $global_groups ); 
  44. } else { 
  45. wp_cache_add_global_groups( array( 'users', 'userlogins', 'usermeta', 'user_meta', 'useremail', 'userslugs', 'site-transient', 'site-options', 'site-lookup', 'blog-lookup', 'blog-details', 'rss', 'global-posts', 'blog-id-cache', 'networks', 'sites', 'site-details' ) ); 
  46. wp_cache_add_non_persistent_groups( array( 'counts', 'plugins' ) ); 
  47.  
  48. if ( did_action( 'init' ) ) { 
  49. $wp_roles = new WP_Roles(); 
  50. $current_user = wp_get_current_user(); 
  51. $current_user->for_blog( $new_blog ); 
  52.  
  53. /** This filter is documented in wp-includes/ms-blogs.php */ 
  54. do_action( 'switch_blog', $new_blog, $prev_blog_id ); 
  55. $GLOBALS['switched'] = true; 
  56.  
  57. return true;