If you want to run over multiple blogs there is no need to restore the previous blog each time. The only thing that grows is $GLOBALS['_wp_switched_stack']
– an array with blog IDs, nothing to worry about.
But keep in mind, restore_current_blog()
will not work ( ! ! ! ) anymore after the second switch, because it uses the previous blog – which is not the first blog then. So store the first blog ID, and call …
switch_to_blog( $first_blog_id );
unset ( $GLOBALS['_wp_switched_stack'] );
$GLOBALS['switched'] = false;
… instead of restore_current_blog()
when you are done. The global variables must be reset, or you will run into the issues mentioned by @user42826.
The performance impact is huge. I’ve run some tests on a local installation with 12 sites:
$sites = wp_get_sites();
print '<pre>' . count( $sites ) . " sites\n";
timer_start();
print 'With restore_current_blog(): ';
foreach ( $sites as $site ) {
switch_to_blog( $site[ 'blog_id' ] );
restore_current_blog();
}
timer_stop( 1, 9 );
print "\nWithout restore_current_blog(): ";
timer_start();
$current_site = get_current_blog_id();
foreach ( $sites as $site ) {
switch_to_blog( $site[ 'blog_id' ] );
}
switch_to_blog( $current_site );
$GLOBALS['_wp_switched_stack'] = array();
$GLOBALS['switched'] = FALSE;
timer_stop( 1, 9 );
print '</pre>';
Result:
12 sites
With restore_current_blog(): 0.010648012
Without restore_current_blog(): 0.005203962
Using restore_current_blog()
after each switch doubles the time that is needed just for switching.