WordPress.org

Codex

Interested in functions, hooks, classes, or methods? Check out the new WordPress Code Reference!

WPMU Functions/switch to blog

Note: If you're using WordPress Multisite, you can instead use switch_to_blog()

Description

Switches the active blog until the user calls the restore_current_blog() function. This function is useful if you need to pull posts, or other information, from other blogs, you can then switch back after using restore_current_blog(). Using this function will not load plugins that only run on the blog you switch to.

This switch is to be used for internal and admin area functions. It's too expensive a query to run on the front end.

Parameters

$new_blog
(integer) (required) The id of the blog you want to switch to.
Default: None

Return Values

(boolean) 
True on success, false if new_blog was blank, or if you tried to switch to the blog you're on.
(global) 
Sets up a global variable called: switched.

Usage

<?php switch_to_blog($new_blog?>

Examples

<?php
    
/* 
       for global variables, since it is being changed or updated from time to time,
       please refer to Related Resources for more information
    */
    
global $switched;
    
switch_to_blog(7);
    echo 
'You switched from blog ' $switched ' to 7';
    
restore_current_blog();
    echo 
'You switched back.';
?>

Source File

switch_to_blog() is located in /wp-includes/ms-blogs.php.

Related Functions

To easily revert back to the blog you were on, restore_current_blog()

Related Resources

Please refer the following links for related variables.WPMU_Global_Variables