Web Analytics

Switch_to_Blog: A Powerful WordPress Multisite Tool

by Jason Unger, Founder

We’ve worked quite a bit with WordPress Multisite, the built-in functionality that allows you to create sub-sites off of your main website. These sub-sites are accessible and managed separately, and can either have their own theme or use the same theme as the primary site.

(There’s plenty we can talk about with the benefits and challenges of Multisite, but we’ll leave that for another post.)

One of the primary requests we get when building out a Multisite install is that there be consistent elements throughout the entire network of sites, such as the navigation and specific sidebar items. However, Multisite is really designed so that each sub-site is treated as its own site – meaning that the content from one site isn’t easily displayed on the other sites. Sometimes this makes a lot of sense; in other cases,  it’s an obstacle that we need to overcome.

For example, we recently completed a Multisite project with five primary menu dropdowns – one for each of the five sub-sites in the network. The admins on each of the sub-sites control the content that goes into those menus, but it all needs to be displayed on every site (including the primary site).

To make that happen, we need to reach into the database and pull the content from the correct site. Enter switch_to_blog.

Switch_to_Blog: Get What You Need Wherever It Is

As described in the 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.

So if I’m looking at sub-site 3, but I need to display the content from a post in sub-site 1, switch_to_blog is the way to do it. Here’s an example of how it’s used.

switch_to_blog( $blog_id );
// Do something

You’ll notice a couple of things.

  1. You need to insert the ID of the blog you’d like to switch to in the initial reference
  2. When you’re done, you need to restore (aka switch back to) the current blog

Here’s an actual example we’ve used; in this case, we’re pulling information from a custom field of a post in the primary site so that it can be displayed on the sidebar of any of the network sites.

<?php switch_to_blog(1); $meta_values = get_post_meta(4586, 'poll', true); echo $meta_values; restore_current_blog(); ?>

Using Switch_to_Blog for Cross-Network Navigation

In addition to the example above, switch_to_blog is incredibly useful for making sure your navigation shows up correctly across every site. Here’s two examples of how that works.

If your menus are controlled only on the primary site, but need to be displayed on every network site:

<?php switch_to_blog(1); wp_nav_menu( array( 'container' => false, 'menu_id' => 'nav', 'theme_location' => 'primary' ) ); restore_current_blog();?>

If your menus are controlled individually by each site and need to be displayed across the entire network:

<?php switch_to_blog(2); wp_nav_menu( array( 'menu' => 'MENUTWO', 'items_wrap' => '<ul id="main-nav">%3$s</ul>', 'walker' => new themeslug_walker_nav_menu()) ); restore_current_blog(); switch_to_blog(3); wp_nav_menu( array( 'menu' => 'MENUTHREE', 'items_wrap' => '<ul id="main-nav">%3$s</ul>', 'walker' => new themeslug_walker_nav_menu()) ); restore_current_blog(); ?>

(There’s some additional Walker functionality in here that you should edit as appropriate.)

One caveat: switch_to_blog doesn’t work for everything. According to the codex, it doesn’t work for autoloaded option, plugins and the object cache. For the most part, however, it does everything you need to ensure that the content you create on one site of your network is accessible and viewable on every other site.

Avatar photo
About Jason Unger

Jason Unger is the Founder of Digital Ink. He built his first website on Geocities, and hasn't looked back since. Digital Ink tells stories for forward-thinking businesses, mission-driven organizations, and marketing and technology agencies in need of a creative and digital partner.

Other Stories You May Like

What’s your story?

Let’s share it with the world.

Let’s Do This

Close Window
All the Cool Kids are Doing it

Sign Up for the Digital Ink Newsletter

All the cool kids are doing it.