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() 描述



(integer) (必填) The id of the blog you want to switch to.

默认值: current blog


(boolean) (可选) Deprecated argument

默认值: null

switch_to_blog() 参数



true is returned even when no blog exists with a Blog ID of $new_blog, or when "switching" to the current blog.

switch_to_blog() 返回值


One switch

Multiple switches

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() 示例


  • 添加于 版本: MU

switch_to_blog() 历史


switch_to_blog() 函数的代码位于 wp-includes/ms-blogs.php

switch_to_blog() 源文件