Tapping into switch_to_blog() Function Performance
It just happened that in my WordPress-related work I focused mostly on WordPress Multisite. I had a couple of big client projects in the past, then I developed WordPress plugins for multisite specifically, for example this one or this another one and here we go.
So I have quite a picture in my mind how the things should work the best.
In this tutorial I am about to talk about switch_to_blog() function, especially about its usage within a loop:
- How does it work exactly?
- Is it slow?
- Can we improve it somehow?
First of all let me show you a code snippet I am talking about.
$sites = get_sites( array( 'fields' => 'ids' ) );
foreach( $sites as $site_id ) {
switch_to_blog( $site_id );
$page = get_page_by_path( 'about', ARRAY_A );
if( $page ) {
wp_trash_post( $page->ID );
}
restore_current_blog();
}In this example we’re using the following functions within the loop:
switch_to_blog()in order to “switch” to a specific blog within a WordPress Multisite, so the functions we’re using right after it (I meanget_page_by_path()andwp_delete_post()are going to be related to the database tables of this specific blog we’ve switched to. More about WordPress multisite database structure you can read here.restore_current_blog()to revert back to the original blog.
And the question is what if we move restore_current_blog() function outside the loop? So we can switch back just one time instead of a lot of times. But there is a problem – restore_current_blog() always switches to the previous site, but not to the original one, so if we move it after } we’re just going to switch to a previous site in a loop and it doesn’t help at all. But why not to use switch_to_blog() instead to switch to the first one?
$first_blog_id = get_current_blog_id();
$switched_stack = $GLOBALS[ '_wp_switched_stack' ]; // Array( 3, 4, 5, 10, etc )
$is_switched = $GLOBALS[ 'switched' ]; // false or true
foreach( $sites as $site_id ) {
switch_to_blog( $site_id );
$page = get_page_by_path( 'about', ARRAY_A );
if( $page ) {
wp_trash_post( $page->ID );
}
}
switch_to_blog( $first_blog_id );
$GLOBALS[ '_wp_switched_stack' ] = $switched_stack;
$GLOBALS[ 'switched' ] = $is_switched;Let me explain this code to you:
- We’re using
get_current_blog_id()function in order to get the original blog ID before we start doing the switch so after the loop ends we can useswitch_to_blog()one more time to come back to this original site. - Global
$GLOBALS[ 'switched' ]just indicates whether the blog is currently switched or we’re on the original site right now. We’re reset this variable after the loop to its original value (at the least the value that was before we started the loop). $GLOBALS[ '_wp_switched_stack' ]stores the switched blogs (their IDs) in the appropriate order. We also restore this function value to the original one after the loop.
The more sites you have the more you win in performance! Thoughts?
Misha Rudrastyh
Hey guys and welcome to my website. For more than 10 years I've been doing my best to share with you some superb WordPress guides and tips for free.
Need some developer help? Contact me
Hello, Misha! Thank you for this post. I’ve recently been working on a multisite which will be switching between all the sites and then restoring the main site. I’m curious, did you ever come to a conclusion regarding the difference in performance between the “standard” method vs. your own? :)
Hey Daniel, what do you mean? :)
The method described in this post is way more faster
Ah, my apologies! I misunderstood and thought that you were still experimenting and weren’t sure if your method was faster.
In that case, I will definitely implement your code in my project as well! Thank you for your work :)
You’re always welcome 🔥
How / where exactly can you make this update?
In your code, Scot