WordPress Multisite allows us to have a network of blogs. Many times the sites from the network may be related, and also the information they contain.
What to do if you are developing a theme, or a plugin, for one of your sites and you want to query posts from another site?
You can use the function switch_to_blog() to change the database we are using. A way to do this would be:
- Save current blog_id
- Change blog_id with switch_to_blog function
- Do the query (query_posts, get_posts, etc)
- Restore original blog_id
Here is an example code snippet:
global $blog_id; $original_blog_id = $blog_id; //$new_blog_id should contain the blog_id we want to query switch_to_blog( $new_blog_id ); //query instructions ... switch_to_blog( $original_blog_id );
Comments, questions, suggestions?