Using WordPress ‘restore_current_blog()’ PHP function

The restore_current_blog() WordPress PHP function restores the current blog after calling switch_to_blog().

Usage

switch_to_blog(5);
// Your custom code here
restore_current_blog();

Parameters

  • None

More information

See WordPress Developer Resources: restore_current_blog()

Examples

Switch to a different blog and display its site title

This example switches to blog with ID 5, displays its site title, and then restores the current blog.

switch_to_blog(5);
echo get_bloginfo('name');
restore_current_blog();

Retrieve and display a specific post from another blog

This example retrieves post with ID 42 from blog with ID 3, displays the post title, and then restores the current blog.

switch_to_blog(3);
$post = get_post(42);
echo $post->post_title;
restore_current_blog();

List all posts from another blog

This example lists all posts from blog with ID 7, and then restores the current blog.

switch_to_blog(7);
$posts = get_posts();
foreach ($posts as $post) {
    echo $post->post_title . '<br>';
}
restore_current_blog();

Display the most recent post from another blog

This example retrieves and displays the most recent post from blog with ID 2, and then restores the current blog.

switch_to_blog(2);
$latest_post = get_posts(array('numberposts' => 1));
echo $latest_post[0]->post_title;
restore_current_blog();

Switch to another blog, update a post, and restore the current blog

This example switches to blog with ID 4, updates the post with ID 10 by appending “Updated!” to the title, and then restores the current blog.

switch_to_blog(4);
$post = get_post(10);
$new_title = $post->post_title . ' Updated!';
wp_update_post(array('ID' => 10, 'post_title' => $new_title));
restore_current_blog();