# switch_to_blog()

URL: https://chugunov.pro/api-wordpress/functions/switch_to_blog/
Проверено на WordPress 6.9, обновлено 06.08.2026.
Источник: независимый русскоязычный справочник chugunov.pro. Не является официальной документацией WordPress.

Тип: функция.
Появился в версии: MU (3.0.0).

## Сигнатура

```php
switch_to_blog( int $new_blog_id, bool $deprecated = null ): true
```

## Описание

Эта функция полезна, если нужно получить записи или другую информацию из других блогов. Вернуться обратно можно с помощью restore_current_blog() .
PHP-код, загруженный вместе с изначально запрошенным сайтом, например код плагина или темы, не переключается. См. #14941.
См. alsorestore_current_blog()

## Параметры

- `$new_blog_id` `int` — обязательный. ID блога, на который нужно переключиться. По умолчанию: текущий блог.
- `$deprecated` `bool` — необязательный, по умолчанию `null`. Не используется.

## Возвращаемое значение

`true`

## Исходный код

Файл: `wp-includes/ms-blogs.php:500`

```php
function switch_to_blog( $new_blog_id, $deprecated = null ) {
	global $wpdb;

	$prev_blog_id = get_current_blog_id();
	if ( empty( $new_blog_id ) ) {
		$new_blog_id = $prev_blog_id;
	}

	$GLOBALS['_wp_switched_stack'][] = $prev_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_id === $prev_blog_id ) {
		/**
		 * Fires when the blog is switched.
		 *
		 * @since MU (3.0.0)
		 * @since 5.4.0 The `$context` parameter was added.
		 *
		 * @param int    $new_blog_id  New blog ID.
		 * @param int    $prev_blog_id Previous blog ID.
		 * @param string $context      Additional context. Accepts 'switch' when called from switch_to_blog()
		 *                             or 'restore' when called from restore_current_blog().
		 */
		do_action( 'switch_blog', $new_blog_id, $prev_blog_id, 'switch' );

		$GLOBALS['switched'] = true;

		return true;
	}

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

	wp_cache_switch_to_blog( $new_blog_id );

	/** This filter is documented in wp-includes/ms-blogs.php */
	do_action( 'switch_blog', $new_blog_id, $prev_blog_id, 'switch' );

	$GLOBALS['switched'] = true;

	return true;
}
```

## История изменений

- MU (3.0.0) — Introduced.

## Связанные

Использует: [`wp_cache_switch_to_blog`](https://chugunov.pro/api-wordpress/functions/wp_cache_switch_to_blog/), `wpdb::set_blog_id`, `wpdb::get_blog_prefix`, [`get_current_blog_id`](https://chugunov.pro/api-wordpress/functions/get_current_blog_id/), [`do_action`](https://chugunov.pro/api-wordpress/functions/do_action/).
Используется в: `WP_User_Query::get_cache_last_changed`, [`user_can_for_site`](https://chugunov.pro/api-wordpress/functions/user_can_for_site/), [`current_user_can_for_site`](https://chugunov.pro/api-wordpress/functions/current_user_can_for_site/), [`wp_initialize_site`](https://chugunov.pro/api-wordpress/functions/wp_initialize_site/), [`wp_uninitialize_site`](https://chugunov.pro/api-wordpress/functions/wp_uninitialize_site/), [`wp_is_site_initialized`](https://chugunov.pro/api-wordpress/functions/wp_is_site_initialized/), [`get_oembed_response_data_for_url`](https://chugunov.pro/api-wordpress/functions/get_oembed_response_data_for_url/), `WP_Site::get_details`, [`has_custom_logo`](https://chugunov.pro/api-wordpress/functions/has_custom_logo/), [`get_custom_logo`](https://chugunov.pro/api-wordpress/functions/get_custom_logo/), [`wp_get_users_with_no_role`](https://chugunov.pro/api-wordpress/functions/wp_get_users_with_no_role/), [`get_site_icon_url`](https://chugunov.pro/api-wordpress/functions/get_site_icon_url/), `WP_MS_Sites_List_Table::column_blogname`, [`confirm_another_blog_signup`](https://chugunov.pro/api-wordpress/functions/confirm_another_blog_signup/), [`wpmu_delete_user`](https://chugunov.pro/api-wordpress/functions/wpmu_delete_user/), [`upload_space_setting`](https://chugunov.pro/api-wordpress/functions/upload_space_setting/), [`wpmu_delete_blog`](https://chugunov.pro/api-wordpress/functions/wpmu_delete_blog/), `WP_Users_List_Table::get_views`, `WP_Importer::set_blog`, `WP_User::get_role_caps`, `WP_Theme::get_allowed_on_site`, [`get_home_url`](https://chugunov.pro/api-wordpress/functions/get_home_url/), [`get_site_url`](https://chugunov.pro/api-wordpress/functions/get_site_url/), [`wp_admin_bar_my_sites_menu`](https://chugunov.pro/api-wordpress/functions/wp_admin_bar_my_sites_menu/), [`count_users`](https://chugunov.pro/api-wordpress/functions/count_users/), [`newblog_notify_siteadmin`](https://chugunov.pro/api-wordpress/functions/newblog_notify_siteadmin/), [`create_empty_blog`](https://chugunov.pro/api-wordpress/functions/create_empty_blog/), [`get_blog_post`](https://chugunov.pro/api-wordpress/functions/get_blog_post/), [`add_user_to_blog`](https://chugunov.pro/api-wordpress/functions/add_user_to_blog/), [`remove_user_from_blog`](https://chugunov.pro/api-wordpress/functions/remove_user_from_blog/), [`get_blog_permalink`](https://chugunov.pro/api-wordpress/functions/get_blog_permalink/), [`get_blog_option`](https://chugunov.pro/api-wordpress/functions/get_blog_option/), [`add_blog_option`](https://chugunov.pro/api-wordpress/functions/add_blog_option/), [`delete_blog_option`](https://chugunov.pro/api-wordpress/functions/delete_blog_option/), [`update_blog_option`](https://chugunov.pro/api-wordpress/functions/update_blog_option/), [`get_blog_details`](https://chugunov.pro/api-wordpress/functions/get_blog_details/), `wp_xmlrpc_server::wp_getUsersBlogs`.

Оригинал в официальной документации: https://developer.wordpress.org/reference/functions/switch_to_blog/
