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

Switch the current blog.


Description Description

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 Parameters

$new_blog

(int) (Required) The id of the blog you want to switch to. Default: current blog

$deprecated

(bool) (Optional) Deprecated argument

Default value: null


Top ↑

Return Return

(true) Always returns True.


Top ↑

Source Source

File: wp-includes/ms-blogs.php

function switch_to_blog( $new_blog, $deprecated = null ) {
	global $wpdb, $wp_roles;

	$blog_id = get_current_blog_id();
	if ( empty( $new_blog ) ) {
		$new_blog = $blog_id;
	}

	$GLOBALS['_wp_switched_stack'][] = $blog_id;

	/*
	 * If we're switching to the same blog id that we're on,
	 * set the right vars, do the associated actions, but skip
	 * the extra unnecessary work
	 */
	if ( $new_blog == $blog_id ) {
		/**
		 * Fires when the blog is switched.
		 *
		 * @since MU
		 *
		 * @param int $new_blog New blog ID.
		 * @param int $new_blog Blog ID.
		 */
		do_action( 'switch_blog', $new_blog, $new_blog );
		$GLOBALS['switched'] = true;
		return true;
	}

	$wpdb->set_blog_id( $new_blog );
	$GLOBALS['table_prefix'] = $wpdb->get_blog_prefix();
	$prev_blog_id = $blog_id;
	$GLOBALS['blog_id'] = $new_blog;

	if ( function_exists( 'wp_cache_switch_to_blog' ) ) {
		wp_cache_switch_to_blog( $new_blog );
	} else {
		global $wp_object_cache;

		if ( is_object( $wp_object_cache ) && isset( $wp_object_cache->global_groups ) ) {
			$global_groups = $wp_object_cache->global_groups;
		} else {
			$global_groups = false;
		}
		wp_cache_init();

		if ( function_exists( 'wp_cache_add_global_groups' ) ) {
			if ( is_array( $global_groups ) ) {
				wp_cache_add_global_groups( $global_groups );
			} else {
				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' ) );
			}
			wp_cache_add_non_persistent_groups( array( 'counts', 'plugins' ) );
		}
	}

	if ( did_action( 'init' ) ) {
		$wp_roles = new WP_Roles();
		$current_user = wp_get_current_user();
		$current_user->for_blog( $new_blog );
	}

	/** This filter is documented in wp-includes/ms-blogs.php */
	do_action( 'switch_blog', $new_blog, $prev_blog_id );
	$GLOBALS['switched'] = true;

	return true;
}

Top ↑

Changelog Changelog

Changelog
Version Description
MU Introduced.


Top ↑

User Contributed Notes User Contributed Notes

  1. Skip to note content
    Contributed by Codex

    Multiple switches

    foreach( $blog_ids as $blog_id ){
        switch_to_blog( $blog_id );
        //Do stuff
        restore_current_blog();
    }
    

    If you do not call restore_current_blog() after every switch_to_blog(), WordPress can get into a state that can potentially build the wrong urls for the site. See restore_current_blog() vs switch_to_blog().

  2. Skip to note content
    Contributed by Store Locator Plus

    Description

    Restores previous blog after a switch_to_blog call.

    Contrary to the function’s name, this does NOT restore the original blog but the previous blog. Calling `switch_to_blog()` twice in a row and then calling this function will result in being on the blog set by the first `switch_to_blog()` call.

You must log in before being able to contribute a note or feedback.