Languages: English • 関数リファレンス/switch to blog 日本語 (Add your language)
Switch the current blog to a different blog. switch_to_blog() is useful if you need to pull posts or other information from other blogs.
You can switch back afterwards using restore_current_blog(). Note that this function reverses only the last blog switching action, typically the most recent call to switch_to_blog(). See the example below on how to proceed when multiple switch_to_blog()s are used.
Things that aren't switched:
switch_to_blog( $blog_id ); // Do something restore_current_blog();
$blog_ids=get_sites(); foreach( $blog_ids as $b ){ switch_to_blog( $b->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()
switch_to_blog() is located in wp-includes/ms-blogs.php
switch_to_blog(), ms_is_switched(), restore_current_blog(), get_current_blog_id(), wp_get_sites(), get_blog_details()