Get content from another site on a WordPress Multisite


Categories
Tags

On a WordPress Multisite you might want to grab content from one of your websites (site 1) and display it on another (site 2). In this tutorial we’ll show you how to switch to another site and get the title, excerpt and main body content, then display it on one of your other sites.

The Switch

As part of a WPMU (WordPress Multisite) setup there’s fantastically useful function called switch_to_blog(), which you can use in your theme files. Firstly, you’ll need to find out the ID of the site you want to switch to. You can do this in the WordPress admin area by going to Sites > All Sites, then hover on one of your blogs/sites, you should see the blog ID in the link. For this example we’re going to switch to a site with the ID of 1. Here’s how you you can use the function to switch sites:

switch_to_blog(1); //switch to blog/site 1

Next, you’ll need the ID number of a post on site 1 that you want to get some content from. Again, you can find this out in the admin area by hovering over the title of your post under Posts > All Posts. We’ll put this ID (250 for this example) into a variable so we can use it when calling the post’s content. We’ll also get the title and the excerpt too and wrap them with a h1 tag and a p tag:

$post_id = 250;	//set the post ID that you want to get

echo '<h1>' . the_title(); . '</h1>'; //title
echo '<p>' . the_excerpt(); . '</p>'; //excerpt

//get the post content and display it
$content_post = get_post($post_id);
$content = $content_post->post_content;
$content = apply_filters('the_content', $content);
$content = str_replace(']]>', ']]>', $content);
echo $content;

Lastly, we need to switch back to our original site. There’s a ready made function for that too:

restore_current_blog(); //restore previous blog/site

Here’s all of the code in one snippet:

switch_to_blog(1); //switch to blog/site 1

$post_id = 250;	//set the post ID that you want to get

echo '<h1>' . the_title(); . '</h1>'; //title
echo '<p>' . the_excerpt(); . '</p>'; //excerpt

//get the post content and display it
$content_post = get_post($post_id);
$content = $content_post->post_content;
$content = apply_filters('the_content', $content);
$content = str_replace(']]>', ']]>', $content);
echo $content;

restore_current_blog(); //restore previous blog/site

In what ways have you used the switch? Let us know in the comments below.


More about Multisite

A multisite network is a collection of sites that all share the same WordPress installation. They can also share plugins and themes.
WordPress Codex

Comments

We welcome comments! Please read the disclaimer.

Disclaimer and Legal Information

The use of any content found on or via this website (code, how to guides etc.) is done so at your own risk. We take no responsibility for any issues encountered.

This blog and its authors will not be held responsible for any misuse, reuse, recycled and cited/uncited copies of content from this website by others.

The views and opinions expressed in this blog are those of the authors and do not necessarily reflect the position or opinion of any other agency, organisation, employer or company.

Comments are moderated but we do not take any responsibility for any libel or litigation that results from something written in a comment. We reserve the right to reject or delete any comment for any reason whatsoever (abusive, profane, rude etc). Please keep your comments polite and relevant.

We are happy for you to quote and share our content in any reasonable manner, e.g. post links to our blogs on social media, but not in any way that suggests that we, or our authors, endorse you, your use or your views.

We appreciate attributions, e.g. a link to our website (www.dragoncode.co.uk).