Make WordPress Core

Opened 3 years ago

Last modified 10 months ago

#25293 new defect (bug)

Switch_to_blog not switching the siteid

Reported by: Rahe Owned by:
Milestone: Future Release Priority: normal
Severity: minor Version: 3.0
Component: Networks and Sites Keywords:
Focuses: multisite Cc:


When having multiple network on multisite making the following:


$options = get_site_option( 'my_option' );


The options retrieved are the options of the current siteid and not the siteid of the switched blog.
One of the options is to make something like this :

global $wpdb;
// Get the previous siteid
$previous_site_id = $wpdb->siteid;
$previous_blog_id = $wpdb->blogid;
// Go to site 1
// Set the blog siteid to 1
$wpdb->set_blog_id( 1, 1 );

// Get the options
$options = get_site_option( 'my_option' );
$wpdb->set_blog_id( $previous_blog_id , $previous_site_id );


// Get the previous siteid
$site_id = $wpdb->siteid;
// Set the blog siteid to 1
$wpdb->set_blog_id( $wpdb->blogid, 1 );
// Get the options
$options = get_site_option( 'my_options' );
$wpdb->set_blog_id( $wpdb->blogid , $site_id );

The thing is that the switch_to_blog function does not specify the switched siteid on the method $wpdb->set_blog_id if the network is not the same as the current blog.

Change History (6)

#1 @jeremyfelt
3 years ago

  • Version changed from 3.6.1 to 3.0

One of the great things about switch_to_blog() is that it requires no database operation to succeed. It is passed a $blog_id (which should be named $site_id), which it sets to the $_GLOBALS['blog_id'], it performs $wpdb->set_blog_id() to get the correct table prefixes setup for when queries are made, and it loads in the user's capabilities for the new $blog_id.

I don't think we want to introduce a database call to find the site's network ID from the $blog_id that was passed. It would be interesting to have switch_to_blog() accept another parameter for $network_id so that the caller could explicitly determine what site and network should be available. The deprecated parameter that is there now was $validate until [13125] it looks like.

More refreshing would be a new switch_to_site( $site_id, $network_id ) function that wraps switch_to_blog() and a switch_to_network( $network_id ) function to try and limit some of the confusion at the code level. Even then, it may be that passing $network_id is a decision left to the developer as the one who knows whether a network level lookup will be needed.

#2 @jeremyfelt
3 years ago

  • Milestone changed from Awaiting Review to Future Release

#3 @jeremyfelt
2 years ago

  • Component changed from Multisite to Networks and Sites
  • Focuses multisite added

This ticket was mentioned in Slack in #core-multisite by jeremyfelt. View the logs.

14 months ago

#5 @GregLone
11 months ago

In 10 days, we'll celebrate this ticket 2y anniversary >_>

This ticket was mentioned in Slack in #core-multisite by jeremyfelt. View the logs.

10 months ago

Note: See TracTickets for help on using tickets.