switch_to_blog [ WordPress Function ]
| Parameters: |
|
| See: | |
| Returns: |
|
| Defined at: |
|
Switch the current blog.
This function is useful if you need to pull posts, or other information, from other blogs. You can switch back afterwards using restore_current_blog().
Things that aren't switched: - autoloaded options. See #14992 - plugins. See #14941
Source
<?php
function switch_to_blog( $new_blog, $validate = false ) {
global $wpdb, $table_prefix, $blog_id, $switched, $switched_stack, $wp_roles, $wp_object_cache;
if ( empty($new_blog) )
$new_blog = $blog_id;
if ( $validate && ! get_blog_details( $new_blog ) )
return false;
if ( empty($switched_stack) )
$switched_stack = array();
$switched_stack[] = $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 ( $blog_id == $new_blog ) {
do_action( 'switch_blog', $blog_id, $blog_id );
$switched = true;
return true;
}
$wpdb->set_blog_id($new_blog);
$table_prefix = $wpdb->prefix;
$prev_blog_id = $blog_id;
$blog_id = $new_blog;
if ( is_object( $wp_roles ) ) {
$wpdb->suppress_errors();
if ( method_exists( $wp_roles ,'_init' ) )
$wp_roles->_init();
elseif ( method_exists( $wp_roles, '__construct' ) )
$wp_roles->__construct();
$wpdb->suppress_errors( false );
}
if ( did_action('init') ) {
$current_user = wp_get_current_user();
if ( is_object( $current_user ) )
$current_user->for_blog( $blog_id );
}
if ( is_object( $wp_object_cache ) && isset( $wp_object_cache->global_groups ) )
$global_groups = $wp_object_cache->global_groups;
else
$global_groups = false;
wp_cache_init();
if ( function_exists('wp_cache_add_global_groups') ) {
if ( is_array( $global_groups ) )
wp_cache_add_global_groups( $global_groups );
else
wp_cache_add_global_groups( array( 'users', 'userlogins', 'usermeta', 'user_meta', 'site-transient', 'site-options', 'site-lookup', 'blog-lookup', 'blog-details', 'rss', 'global-posts' ) );
wp_cache_add_non_persistent_groups(array( 'comment', 'counts', 'plugins' ));
}
do_action('switch_blog', $blog_id, $prev_blog_id);
$switched = true;
return true;
}
?>
Examples [ wp-snippets.com ]
Top Google zoekresultaten
- WPMU Functions/switch to blog « WordPress Codex
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, ...
codex.wordpress.org - Function Reference/switch to blog « WordPress Codex
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 ...
codex.wordpress.org - using switch_to_blog
Hi, I'd like to use the WPMU function switch_to_blog and retrieve some posts from another blog.
www.wptavern.com - 3.0 - Why would switch_to_blog stop working? - WordPress - Stack ...
Nov 29, 2010 ... It appears that switch_to_blog might be too unpredictable to rely on for major site design. Here's my first attempt at a SQL-based solution.
wordpress.stackexchange.com
Gebruikersdiscussies [ wordpress.org ]
- Ipstenu on "Permalink displays incorrect URL and not support switch_to_blog function"
- parag25 on "Permalink displays incorrect URL and not support switch_to_blog function"
- Ipstenu on "Replacing switch_to_blog"
- sunburntkamel on "Replacing switch_to_blog"
- sunburntkamel on "Replacing switch_to_blog"
- Andrea_r on "Replacing switch_to_blog"
- sunburntkamel on "Replacing switch_to_blog"
- Ipstenu on "Replacing switch_to_blog"
- sunburntkamel on "Replacing switch_to_blog"
- Curtiss Grymala on "Any way to retrieve accurate list of post types after switch_to_blog()?"