Get Posts or Terms from All Blogs in Multisite Network

How to query all posts or terms in a single loop within WordPress Multisite install. With working pagination.

Query All Network Posts

If you're familiar with WP_Query class, you will be stunningly surprised. Network_Query is its multisite analogue.

$args = array(
	'posts_per_page' => 50,
	'orderby' => 'name',
	'order' => 'ASC'
	// all WP_Query arguments are supported including meta_query, tax_query etc.
);
 
$network_query = new Network_Query( $args ); // just like WP_Query!
 
if( $network_query->have_posts() ) :
 
	while( $network_query->have_posts() ) : $network_query->the_post();
 
		// $network_query->post is a post object - print_r( $network_query->post ) to find out more
		// use build-in functions like network_get_permalink() and network_get_post_thumbnail() to get post URL and thumbnail accordingly
		echo '<h3 id="post-' . $network_query->post->ID . '" class="blog-' . $network_query->post->BLOG_ID . '">
			<a href="' . network_get_permalink( $network_query ) . '">' . $network_query->post->post_title . '</a>
			</h3>';
 
	endwhile;
 
else:
	echo 'No posts in the network for this criteria.';
endif;

Global Search

One search form. Multiple blogs search resuts.
You can search for posts, pages, tags, categories, custom post types and taxonomies.

Multiple blogs search results

Customize

Choose Post Types and Blogs to Index / to Query

post types and global search customization

Unlimited Blogs

Hundreds of blogs in your network? Not a problem — my plugin uses cron jobs to prevent server overload.

Unlimited number of blogs is supported

Support

I'm always here if you need my help.
Please create a ticket if you need assistance with my plugins or contact me if you have a question.

$args = array(
	'posts_per_page' => 50,
	'orderby' => 'name',
	'order' => 'ASC'
	// all WP_Query arguments are supported including meta_query, tax_query etc.
);
 
$network_query = new Network_Query( $args ); // just like WP_Query!
 
if( $network_query->have_posts() ) :
 
	while( $network_query->have_posts() ) : $network_query->the_post();
 
		// $network_query->post is a post object - print_r( $network_query->post ) to find out more
		// use build-in functions like network_get_permalink() and network_get_post_thumbnail() to get post URL and thumbnail accordingly
		echo '<h3 id="post-' . $network_query->post->ID . '" class="blog-' . $network_query->post->BLOG_ID . '">
			<a href="' . network_get_permalink( $network_query ) . '">' . $network_query->post->post_title . '</a>
			</h3>';
 
	endwhile;
 
else:
	echo 'No posts in the network for this criteria.';
endif;

Global Multisite Search and Custom Post Queries

Just for 39$

Get the plugin

So, How to Get All Network Posts in a One Query?

Click the links below to jump to a specific part of the documentation.

A few notes about plugin installation

This plugin will index all posts in your Multisite Install with custom fields meta_query and terms tax_query. The posts and terms index will be stored in the following database tables.

phpMyAdmin: MySQL database tables with posts and terms indexed from all sites in a network.
MySQL database with the global content inside phpMyAdmin

Now some notes about the plugin installation.

  1. You can download the plugin by clicking the button in the top right part of the screen.
  2. By default this plugin will index only the new posts you publish. If you want to add to the index your old posts, go to Settings > Index and then switch to Rebuild Index tab.
Go to the Rebuild Index tab if you want to add your old posts to the network index.
That’s where you can completely rebuild the index.

One important thing to know — when you rebuild your global index, it may take a time — how much — depends on the size of your Network. Maybe 10 minutes or maybe an hour.

You can find the index stats in your Network Dashboard.

You can find the plugin index statistics in Network Dashboard.

Example 1. Just Get the Posts like WP_Query

Let me introduce you the awesome Network_Query class — it works just like WP_Query but for all the network websites and supports all the same parameters + blog_id parameter (I will show how this parameter works in the next example).

Now let’s begin with something simple.

$network_q = new Network_Query('posts_per_page=5'); // display 5 latest global posts
 
if( $network_q->have_posts() ) :
 
	// run the loop for each post
	while( $network_q->have_posts() ) : $network_q->the_post();
 
		// we just get post titles here
		// but you can print_r( $network_q->post ) to view all post data
		echo '<h3>' . $network_q->post->post_title . '</h3>';
 
	endwhile;
 
else :
	echo 'No posts found for this criteria';
endif;

Something a little more interesting.

// Network_Query parameters
$args = array(
	'posts_per_page' => 14,
	'orderby' => 'name',
	'order' => 'ASC',
	'meta_query' => array(
		array(
			'key' => 'featured', // we display only posts with custom field 'featured' = 'on'
			'value' => 'on'
		)
	)
);
 
$network_q = new Network_Query( $args );
 
// if there are posts, then print <ul>
if( $network_q->have_posts() ) :
	echo '<ul>';
 
	// run the loop
	while( $network_q->have_posts() ) : $network_q->the_post();
 
		// the get_permalink() function won't work without switch_to_blog()
		// you can use network_get_permalink() instead but it is a little slower
		switch_to_blog( $network_q->post->BLOG_ID );
 
		// you can obtain the post title from $network_q->post object
		echo '<li class="post-' . $network_q->post->ID . ' blog-' . $network_q->post->BLOG_ID . '">
			<a href="' . get_permalink( $network_q->post->ID ) . '">' . $network_q->post->post_title . '</a>
		</li>';
 
		// restore_current_blog() to switch to the previous (!) website
		restore_current_blog();
	endwhile;
 
	echo '</ul>';
endif;
network_reset_postdata(); // add it after the loop if you plan to use Network_Query multiple times on the page

Some comments:

The next very important feature of the plugin is the global search. And you can search not only for posts and pages (post types), you can also perform search for categories and post tags (taxonomies).

Starting with the plugin version 4.4 it is even not required to do anything in the code in order to enable the global search. Just go to Settings > Index and then proceed to the Globals tab.

If you use the widget option, the global search widget will  appear in Appearance > Widgets, or if you would like to replace the default search results, you should also proceed to the Sites admin page and turn it on there as well.
If you use the Widget option, the Global Search Widget will appear in Appearance > Widgets, or if you would like to replace the default search results with the global ones, you should proceed to the Sites admin page and turn this feature on there for each site individually.

With the code you also can:

In full global search tutorial I will explain you each of the above examples and show how to replace default WordPress search form and results page with the global ones.

Everything else is like in WP_Query.

Example 3. Get Posts only from the specific blogs in a network. Choose post types to index / to query

Actually you can:

post_type
(string|array) It is default WP_Query parameter and I suppose you should already know how it works.
$args = array( 'post_type' => array( 'post', 'page' ) ); // get only posts and pages
blog_id
(int|array) Specify the IDs of the blogs you want to query posts from.
$args = array( 
	'post_type' => 'post', // only posts
	'blog_id' => array( 1, 3 ) // only from blogs 1 and 3
);

Do not know where to get blog ID? Not a problem — my plugin will show a blog ID column in your network dashboard Sites > All sites.

Column with Blog ID on the All Sites page
Plugin automatically adds the Blog ID column to the table with the all websites.

You should also know one thing about post_type query parameter — it will work only for indexed post types, for example here I added to the index the product post type in the plugin settings:

Add the WooCommerce product post type to global network index.

And as I mentioned before, the post types for indexing can be configured individually for each network website. To do this, in your multisite dashboard go to Sites > All sites and click Configure link under the Indexing tab.

Here you can configure what post types to index for each blog individually.
For each blog you can choose what post types to index or completely disable indexing.

Example 4. Query Network Posts with Pagination

It is very important for me to implement the pagination for network posts. And this is the simple way how you can do it:

global $wp_query;
 
// get the current page number from $wp_query, I mean the URL parameter, i.e. /page/2
$current_page = (get_query_var('paged')) ? get_query_var('paged') : 1; // you can use $wp_query->query['paged'] as well
 
// similiar to query_posts()
$network_q_posts = network_query_posts( array('posts_per_page' => 3, 'paged' => $current_page) ); 
 
// run the loop
foreach( $network_q_posts as $network_q_post ) :
 
	// we need it to work with get_the_post_thumbnail() and get_permalink()
	switch_to_blog( $network_q_post->BLOG_ID );
 
	echo '<li>' . get_the_post_thumbnail( $network_q_post->ID ) . '<a href="' . get_permalink( $network_q_post->ID ) . '">' . $network_q_post->post_title . '</a></li>';
 
	// switch back
	restore_current_blog();
endforeach;
 
// we should change the global $wp_query value to work correctly with pagination
$wp_query = $GLOBALS['network_query'];
 
// I use the popular WP PageNavi plugin
wp_pagenavi();
 
// reset the $wp_query
wp_reset_query();

If you have problems with this example, you can read full detailed tutorial here.

Example 5. Get categories, post tags globally using get_terms() analogue

My plugin allows you to get not only post types but taxonomy terms as well. It can be implemented easily with network_get_terms() (click the link to view the detailed tutorial).

And here is a simple example:

$network_categories = network_get_terms('category', 'orderby=name&hide_empty=0');
 
if( $network_categories ){
	echo '<select>';
	foreach ( $network_categories as $network_category ){
		echo "<option value='{$network_category->term_id}'>{$network_category->name}</option>";
	}
	echo '</select>';
}
/*
in this example is obvious that
$network_category->name - category name,
$network_category->term_id - category global unique ID across the network
but you can get more info about the category if you look into the object using
print_r( $network_category );
*/

Changelog

5.4 – Mar 25, 2019

5.2 – Oct 25, 2018

5.1.1 – Sep 25, 2018

5.1 – Sep 14, 2018

5.0 – Apr 19, 2018

4.8.3 – Mar 10, 2018

4.8.2 – Feb 18, 2018

4.8.1 – Feb 14, 2018

4.8 – Feb 10, 2018

4.7.1 – Sep 18, 2017

4.7 – Sep 16, 2017

4.5 – July 25, 2017

4.4.1 – December 5, 2016

4.4 – December 4, 2016

4.3.1 – November 15, 2016

4.3 – November 7, 2016

4.2 – September 23, 2016

4.1.2

4.1.1

4.1

4.0

3.1

What people are saying in Twitter

BEST solution to easily work with WP multisite - True Multisite Indexer plugin! Thanks @rudrastyh for your plugin a… https://t.co/VJLcled9xO
Alexandre Gaboriau @alexgaboriau
I would like to highlight @rudrastyh works with his WP plugins. He has been really very available. As WordPress pro… https://t.co/c8psjKPCdL
Michael Michelini @michelini
@rudrastyh amazing multisite plugin and even better support- Christmas day reply from Misha the developer, thanks!
@rudrastyh awesome plugin & awesome support https://t.co/nZFSGk0PPD
Pablo Rodríguez @pablo_r_f
Thanks to @rudrastyh for the great support given in his plugin for Multisite Post Pagination for @WordPress 👌🏽
Valentin GUENICHON @IFight4TheUsers
Awesome support from @rudrastyh for his essential plugin "the #wordpress network query for multisite" https://t.co/WqYCXFblIB
Fikri Mastor @FikriMastor
I've found best solution to pull all sub-site blog in network into main site. Thanks to @rudrastyh develop #WordPress True Multisite Indexer
Daryl Glass @darylglass
Thank you @rudrastyh for the great plugin, intuitive tutorial and efficient service. I am very happy :) Great job! #wordpress #multisite
Mo @M6J33D
Get Posts or Terms from All Blogs in Multisite Network. Awesome plugin by @rudrastyh! https://t.co/JSzBppO7KE

Some of the companies that use this plugin

Make Do (Barnsley, UK)Modularte modularte.de (Hanau, Germany)Connective DX (Sydney, Australia)Old River Creative (Woodstock, Virginia, USA)
DotDev (Australia, Brown Hill)Miux (Switzerland, Chur)PurposeWP (United States, Fort Worth)Rovecom (Netherlands, Hoogeveen)

14 Day Money Back Guarantee

Secure Checkout. Instant Download

Recommended

Developer

$139 one-time payment
  • Lifetime priority support
  • Lifetime updates
  • Unlimited websites

Personal

$39 one-time payment
  • 2 weeks/support
  • 1 year/updates
  • 1 website

View my refund policy here

The plugin is a one-time purchase which includes a license for plugin updates and support. To continue receiving updates & support after your license has expired you may renew.