Misha Rudrastyh

Misha Rudrastyh

WordPress Developer

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.

Get the plugin
phpMyAdmin: MySQL database tables with posts and terms indexed from all sites in a network.Recently indexed posts and terms. You can access these stats in Settings > Multisite Index.Go to the Rebuld Index tab if you want to add your old posts to the network index.Add the attachment post type to global network index.Here you can configure what post types to index for each blog individually.
View video
  • Use Network_Query to get all blog posts (supports all WP_Query parameters)
  • Use s= parameter of the Network_Query to search the posts over the network or search posts by their custom field values using meta_query
  • Configure what post types to index for each blog individually
  • Contact me if you will need help with any of this
  • Use network_get_terms() to get terms from all network sites (supports all get_terms() parameters)
  • Search terms from the network by part of their titles or descriptions using name_like and description_like parameters of network_get_terms() function
  • Use blog_id paramater of the Network_Query to get posts from the specific blog(s)

In a one beautiful day I needed to query posts from all the blogs in my WP Multisite Install. The posts should be within one loop (!) and the pagination should work (!) as well.

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

Install the Plugin

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.

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 > Multisite Index» and then switch to «Rebuild Index» tab.
    Go to the Rebuld Index tab if you want to add your old posts to the network index.

And now I will show you two simple examples.

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.

(int|array) Specify the IDs of the blogs you want to query posts from.
// everything works like WP_Query
$network_q = new Network_Query('posts_per_page=14&orderby=name&order=ASC');
// if there are posts, then print <ul>
if( $network_q->have_posts() ) :
	echo '<ul>';
	// the loop
	while( $network_q->have_posts() ) : $network_q->the_post();
		// the get_permalink() function won't work without switch_to_blog()
		switch_to_blog( $network_q->post->BLOG_ID );
		// you can obtain the post title from $network_q->post object
		echo '<li><a href="' . get_permalink( $network_q->post->ID ) . '">' . $network_q->post->post_title . '</a></li>';
		// restore_current_blog() to switch to the previous (!) website
	echo '</ul>';

Some comments:

  • In the loop we obtain $network_q->post object — it has all WP_Post object parameters with $network_q->post->BLOG_ID, the ID of the network blog.
  • If you need to do something and the WP_Post object is not enough for your purposes, then use the switch_to_blog() function.
  • We need restore_current_blog() to cancel the switch_to_blog(), the function switches us to the previous blog — so, use it in a loop.
  • You should also know one thing about post_type query parameter — it will work only for indexed post types, for example I added to index the attachment post type in the plugin settings:
    Add the attachment post type to global network index.

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

    Here you can configure what post types to index for each blog individually.
  • The network_reset_postdata() works like wp_reset_postdata().

Everything else is like in WP_Query.

Example 2. Query Network Posts with Pagination

It is very important for me to implement the pagination for network posts.

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
// 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
// reset the $wp_query

To understand how the network_query_posts() works, lets look at this table:

WordPress default Multisite
$post $network_post
$wp_query $network_query
$GLOBALS['wp_query'] $GLOBALS['network_query']
WP_Query Network_Query
query_posts() network_query_posts()
wp_reset_postdata() network_reset_postdata()
wp_reset_query() network_reset_query()

Money Back Guarantee!

If for some reason the plugin wouldn’t work on your website – please let me help you to fix it, if I can't, I will simply return your money.

4.2 – September 23, 2016

  • Bug fixes.
  • Plugin update improvements.


  • Now the blog_id parameter of Network_Query supports array values.


  • Fixed redirect location for manual updates check.


  • Code optimization and UI improvements.


  • You can now get terms (categories, post_tags etc) from all blogs in Multisite network in a one loop using network_get_terms() function.
  • Changes in plugin update mechanism.


  • Changes in plugin update mechanism.

Comments 19

  • Hats off to Misha! We’ve been looking for a plugin that searches across our networked sites. Excellent plugin and excellent support. We are very happy with the results.

  • Great job for a multisite, with a small customize to be perfect. Misha is a great developer :)

  • We did not install the plugin yet but Misha is very reactiv, professional and sympathetic ! Recommanded :)

  • Davide De MaestriSeptember 8, 2016 at 12:09

    Great plugin and amazing support, Misha is a really good WP Developer and kind person. Really appreciated!

  • Hello, I just bought the plugin and I have a wpml multisite multilanguage website. I noticed that posts from other languages are between the default language.

    This is not supposed to happen, I want to switch manually to a language and only want to see posts in that language.

    Please help

    • MishaAuthorAugust 10, 2016 at 16:08

      if I understand you properly, currenly all the posts (all languages) are in the index.

      Could you tell me please where do you want to print the posts and how?

      • Yes Misha, that is correct.

        I’m hosting a local wordpress environment. This is the query I use:

        $network_q = new Network_Query(‘posts_per_page=14&orderby=post_date&order=DESC&post_type=page’);

        After that I use the usual loop:

        if( $network_q->have_posts() ) :…. etc.

        • MishaAuthorAugust 10, 2016 at 16:08

          I think you can use one of the following:

          1. Set blog_id as Network_Query paramater

          $args = array(
          	'blog_id' => array( 1, 2 ), // one or more blog IDs to INCLUDE
          	'posts_per_page' => 14,
          	'orderby' => 'post_date',
          	'order' => 'DESC',
          	'post_type' => 'page'
          $network_q = new Network_Query( $args );

          2. Skip the loop if blog ID doesn’t match your task

          while( $network_q->have_posts() ) :
          	if( in_array( $network_q->BLOG_ID, array( '3', '4' ) ) {  // in array specify blog IDs to EXCLUDE
          • That’s not my problem. I want all my blogs, I sometimes have multilanguage posts in 1 blog.

            Here is my problem:

            Some blogs have posts in different languages. I use WPML for it. when I normally use WP_Query it looks at the current language and modifies the query, so it only fetches posts in the current language.

            But when I use Network_Query I get posts in 2 languages, it doesn’t look at the current language the user selected.

            Do you understand?

          • MishaAuthorAugust 10, 2016 at 19:08

            I don’t use WPML for translations, so Im not sure that I can give you a proper advice.

            I think you just need to find out how WPML tweaks WP_Query and then to apply that changes to Network_Query or you can give me the information about it (for example if WPML tweaks it by custom fields or you can say me the name of the action hook) and I add this feature to the plugin by myself.

  • Hi,

    Does the plugin have to access WP_Cron from the outside? Our Multisite network is a closed network (hidden behind VPN) and it is not possible to access from the outside without having logged into VPN.

  • Hrvoje KrbavacJune 17, 2016 at 16:06

    Well done Misha! Contact me if you wish to work on one bigger WP project with me. :)

    • Hrvoje KrbavacJune 17, 2016 at 17:06

      Sorry but you have made a mistake.. at least my opinion :)
      Your plugin works only if you install it and after activation insert terms/categories…
      You should upon activation go through all terms and update your MySQL tables.

      • MishaAuthorJune 18, 2016 at 09:06

        Hello :)

        No, it indexes all the network terms/posts/categories etc. It just doesn’t index all of them at once — just to prevent overload of your blogs.

        So, if WP_Cron is working on your website (on the most of WordPress blogs it works well) the previously created terms and posts will be indexed soon.

  • Perfect plugin, beatifully implemented ;)
    Support is also fast and pleasant. Author is great professional.

    I recommend, it is really worth the money!!!

  • Excellent customer service.
    You need to understand and implement the code examples

Leave comment

Please, enter a comment
Please, enter a name
Incorrect email

Get the plugin


Get this plugin with free updates and unlimited expert WP support.
* for unlimited websites
© 2016 Misha Rudrastyh
Developed on with