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 :D :
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!