WordPress Multisite is a powerful feature that allows multiple virtual sites to share a single WordPress installation.
Many-a-times while working with WordPress multisite, you’ll have to perform an operation across all the sites. For example, accessing and listing posts from all blogs on a single site.
For those who aren’t quite familiar with the way WordPress multisite works, this might seem like a complicated task.
But, it’s quite the contrary.
Once you know how a multisite is structured and how to navigate across the blogs in a network, this task becomes quite simple.
So, without further delay, let’s find out!
[space]
#1 The Database and Tables in WordPress Multisite
A WordPress multisite is a network of blogs connected by a single WordPress installation. So, when we talk about a single WordPress installation, we talk about a single database.
Sure, the tables are different for each blog but they are present in a common database.
The great thing about WordPress is that all tables are created with a prefix, which is the blog id. For example, the posts table for the blogs will be wp_1_posts, wp_2_posts, wp_3_posts… and so on. (Considering wp is the database prefix)
Hence, to access post data belonging to a particular blog, you need to use the blog id prefix and the table name. To access data from a different blog, you just need to switch the blog id prefix but the table name remains the same.
You can get the list of all blogs using the wp_blogs table.
So the next question is, exactly how do we use the wp_blogs table to switch between blogs.
[space]
#2 Switching Between Blogs in WordPress Multisite
There are two main functions you need to know which can help you switch between blogs in a multisite network:
- switch_to_blog: which is used to switch to a particular blog, using the blog id.
- restore_current_blog: this restores the table prefix to the previous blog you were present on when switch_to_blog was last called.
These two functions can help you navigate across blogs in a multisite network. It is always advisable to use these functions sparingly or only in the back end because it’s an expensive query to be invoked on the front end.
So, now coming to the main point. How do we use these two functions and the wp_blogs table to retrieve all blog posts and display them on a particular blog.
[space]
#3 Listing Posts from all Blogs on the Main site
Let’s consider you’re present on the main blog. You can get the current blog id using the global variable $blog_id.
- You have to then get all blog ids by using the wp_get_sites function, which gets all blogs from wp_blogs table.
- Then all you need to do, is loop through these blog ids and switch to each blog and retrieve the blog posts!
It’s that simple!
And just to help you out a bit more, here’s all the code in one place 😀 :
function display_blogs() { // loop through all blogs $all_blog = wp_get_sites(); foreach ($blog_ids as $key=>$current_blog) { // switch to each blog to get the posts switch_to_blog($current_blog['blog_id']); // fetch all the posts $blog_posts = get_posts(array( 'posts_per_page' => -1)); restore_current_blog(); // display all posts } }
[space]
Hope this helps. Remember to call restore_current_blog when using switch_to_blog to avoid any confusion.
In case you have any questions, do let me know in the comment section below.
Have a great weekend!
12 Responses
Do you have a way where i can show related posts from my other website? I am using wordpress multisite.
Hi Prasad,
If you have categorized the posts, then you can fetch posts of a certain category and display them, by adding a condition in the above code.
Hi Sushma B,
I’m a bit slow understanding if it may be helpful for me, cause I’m not very expert and english is not my native language, so please forgive me for the banal questions.
What should be done to display in a main page blog posts grouped in different main categories? Of course I don’t want all the posts to be actually displayed below each category; I’d need first to select which category I’m interested in, and once I’ve selected the category, having all the posts of that category displayed in one page.
Is that possible?
P.S I’d need this method for products of subshops in a multisite scenario for creating a marketplace, but as the above content talks about blog posts, I’ve used these terms in my message as well.
Thanks in advance
Hi Frans,
You could create a dropdown and list all categories in it. On selection of a category you could call a function using AJAX and pass the selected category ID as a parameter. In the callback function you can then need to add a get_posts query as follows:
Thank you so much for writing but can you please tell me how i can implement this in twenty fifteen theme.
Like if i copy this in my twentyfifteen wordpress theme so where i need to call this in my index.php
I am newbie so please reply soon
Best Regards
Shahroz
Hi Shahroz,
Here’s what you can do. Add the code to functions.php file of your theme. And call this function on a hook based on when you want this displayed. For example, you could create a shortcode and in the callback function add this code.
Hope this helps! 🙂
I have a site in wordpress multisite, what I need is the following:
– List products registered in other stores in my network that will be the client, in a single page. But when the customer clicks goes directly to the customer’s shop and does not make the purchase within the main network ….
Hi,
I realise this is a relatively old post and may not get a reply but thought it was worth a shot to ask anyway. I am getting the following error message when I copy in the code in the functions.php file of my theme: “Warning: Invalid argument supplied for foreach() in functions.php on line 50”. Is this because wordpress now no longer uses $blog_ids or $current_blog? Or am I supposed to declare these variables somewhere else first? I would greatly appreciate any help you can offer.
Thanks, Sofie
function display_blogs() {
// loop through all blogs
$all_blog = wp_get_sites();
foreach ($all_blog as $key=>$current_blog) {
// switch to each blog to get the posts
switch_to_blog($current_blog[‘blog_id’]);
// fetch all the posts
$blog_posts = get_posts(array( ‘posts_per_page’ => -1));
restore_current_blog();
// display all posts
}
}
Why not just use the Network Posts Extended plugin?
HI Sushma,
I have to display all blog posts in home in multisite network, So i have created the custom template to get all posts with below code as per your article
$all_blog = wp_get_sites();
foreach ($all_blog as $key=>$current_blog) {
//echo $current_blog->blogname;
switch_to_blog($current_blog[‘blog_id’]);
$blog_posts = get_posts(array( ‘posts_per_page’ => -1));
//print_r(count($blog_posts));
foreach ($blog_posts as $post) {
}
}
i have thousand’s of blog posts in entire multisite n/w, So i am getting 500 error while displaying the all posts.Can you please suggest how can i get all posts without effecting the performance of site.
2. How to create the pagination for this custom template.
3.Search functionality in the Home page to search the all blogs in the multisite.
Please send me the better suggestions or relevant url’s for these requirements.
Thanks in advance.
yes it is a nice feature and i have also used it in my projects.