Misha Rudrastyh

Misha Rudrastyh

WordPress Developer

How to Search across WordPress Multisite Network

In this tutorial I will show you how to implement global search for all the websites in the whole WordPress Multisite Network (post search, post search by custom field value, term search).

October 5, 2015June 9, 2016 11

To make the following examples work, you need to install the plugin which will index all network posts and terms, it will also add some specific functions to your websites (network_query_posts(), network_get_terms() etc).
Download Plugin

Global Search with s= parameter of network_query_posts() function.

Nothing especial — everything works like WordPress default query_posts() function.

$args = array(
	's' => 'wordcamp', // the search query
	'posts_per_page' => -1
$networkposts = network_query_posts( $args ); // or network_query_posts('s=wordcamp&posts_per_page=-1');
foreach ( $networkposts as $networkpost ):
	echo '<p>' . $networkpost->post_title . '</p>';

How to make your default WordPress search works for all websites in a network

You can do it in two ways:

  • modifying the query in the default search template (all search results will be shown on the one page, with no pagination),
  • creating a custom page template for global search results, with page navigation.

Print the search results in default search template

  1. Go to Appearance > Widgets and add the default WP search widget to any sidebar you want.
    Add the default search widget to the website.
  2. In your current theme folder find and open search.php file.
    search.php in the theme directory

    Then insert at the beginning of the file:

    $args = array(
    	's' => get_search_query(), // or $_GET['s']
    	'posts_per_page' => -1
    $network_posts = network_query_posts( $args );
  3. The last step — make the following replacements in it:
    Before After
    if ( have_posts() ) : if( $network_posts ) :
    while ( have_posts() ) : foreach( $network_posts as $nw_post ) :
    the_post(); switch_to_blog( $nw_post->BLOG_ID );
    endwhile; restore_current_blog(); endforeach;

    Keep in mind that search.php can be different for every WordPress theme.

Print the results in your custom page template

  1. Create an empty file in your current theme directory and name it somehow.
    Example of custom page template file.

    At the beginning of the file insert:

     * Template name: Global Search Results
    $current_page = ( get_query_var('paged') ) ? get_query_var('paged') : 1; // or $wp_query->query['paged']
    $args = array(
    	's' => $_GET['s'], 
    	'posts_per_page' => get_option('paged'),
    	'paged'	=> $current_page
    $network_posts = network_query_posts( $args );
    // ... 
    // search.php

    Then copy your search.php file content and paste it next to those lines.

  2. Create a new page Pages > Add New and apply the template to it.
    Creating a new page with custom page template.
  3. Make the code replacements in this file (from the table before).
  4. Before your pagination function (it can be wp_pagenavi()) insert the line $wp_query = $GLOBALS['network_query'];.
  5. Make sure that your search form has your template URL in the action attribute.
    <form action="http://your-website/global-site-search" method="GET">
    	<input type="text=" name="s" />

You can begin with the simplier way.

Search Posts by their Custom Field Values

I hope you already know about WordPress custom fields. You can also read more about meta_query here.

$args = array(
	'posts_per_page'  => -1, // let's get all posts
	'meta_query' => array(
		'relation' => 'OR',
			'key'     => 'key1', // key1 must contain "wordcamp"
			'value'   => 'wordcamp',
			'compare' => 'LIKE'
			'key'     => 'key2', // OR key2 must contain "wp"
			'value'   => 'wp'
			'compare' => 'LIKE'
$network_q_posts = network_query_posts( $args ); // 
foreach( $network_q_posts as $network_q_post ) :
	echo '<p>' . $network_q_post->post_title . '</p>';

Search Terms by part of their Titles and Descriptions

Read more about network_get_terms() function first.

// search for categories whose descriptions contain "wordcamp"
$network_cats = network_get_terms('category', 'description_like=wordcamp');
if( $network_cats ){
	foreach ( $network_cats as $network_cat ){
		echo $network_cat->name;
// search for post tags whose names contain "wp"
$network_post_tags = network_get_terms('post_tag', 'name_like=wp');
if( $network_post_tags ){
	foreach ( $network_post_tags as $network_post_tag ){
		echo $network_post_tag->name;

Only new posts about WordPress

once a week

Comments 11

  • Hello.
    I purchased the plugin.
    Is it possible to upload a sample?

  • I wanted to perform a search of the following multi-site.
    jobinfo_cat[] = Custom categories

    beauty salons

    It is likely to be supported by custom fields below.

    • MishaAuthorAugust 21, 2016 at 15:08


      sorry — I’ve removed some of your unformatted code.

      First of all — I do not recommend to use pre_get_posts filter for network queries.

      If you would like to use the AJAX filter, then yes, you can try this tutorial. Everything you have to do is to change WP_Query to Network_Query and wp_reset_postdata() to network_reset_postdata().

      That should be enough.

  • Hi
    A question before I buy the plugin.

    Is it possible to have a search form only at the Main site to search for all the multisite posts?
    And then have the default search function on all other sites?

    Do you have any demo page for this plugin?

    Regards Malin

    • MishaAuthorAugust 30, 2016 at 17:08

      Hi Malin,

      yes, it is possible,
      my plugin doesn’t change standart WordPress queries.

      Currently there is no demo page. But I’m already think about how to implement it the best way.

      • Thank you for your quick reply! WOW!

        Okey, that sounds good! How to make a search form for the main site – do you have any tutorial for that?


      • Oh, I’m sorry – I think I’m that page? :-)
        To many hours in this project I think…

        Can I find more information about the search form/result?


        • MishaAuthorAugust 30, 2016 at 17:08

          I think here you can find all the info about it, but if you have a question or a problem – just contact me and I will help you :)

  • HI Misha,

    Just a question about this plugin im consider purchasing it for a multisite blog, can i extend this plugin onto a page rather than adding it to a sidebar? and do the results show on the same page as the search box? i’m looking for a plugin i can use to search a company multisite consisting of 100+ blogs and i was a search bar that can search all the posts on each blog for a specific keyword and return all the results to a single page, is that what this plugin does?

Leave comment

Please, enter a comment
Please, enter a name
Incorrect email
© 2016 Misha Rudrastyh
Developed on with