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).

#WordPress Multisite  /    /   50

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;
Misha Rudrastyh
About the author Misha Rudrastyh

Passionate about WordPress and snowboarding, creating websites for over 10 years! Let's work together — just contact me.

If you are a developer too, subscribe to my facebook page.

Comments 50

← Older
  • Thank you for this wonderful plugin! Instead of having Network_Query() querying through all multisite blog ID’s, how can I restrict it to only query certain blog ID’s, like 2 & 3?

    • Just figured out I can do this ‘blog_id’ => [2,3]. Awesome! :)

      • MishaAuthor

        Hi Daniel,
        great! have a nice day!

        P.S. 'blog_id' => array( 2, 3 ) is also ok :)

  • Hello, I’m interested in purchasing your plugin, but I’m wondering if it works with other plugins that allow users to exclude posts and pages from the search index for the default WordPress search, like Search Exclude for instance. Thank you!

    • MishaAuthor


      I haven’t tested it with Search Exclude plugin.

      • Hi, thanks for your quick response! I’m guessing I could also add an extra argument to exclude posts based on a custom field checkbox. Does your plugin work with Advanced Custom Fields? Thank you!

        • MishaAuthor

          Hi, yes, you can exclude posts based on a custom field value with meta_query parameter that is supported by the plugin.

          Advanced Custom Fields is just a wrapper for WordPress Custom Fields. My plugin works with custom fields.

  • Hi – thanks for tutorial and work on multisite

    Does the indexer create ‘copies’ of data? I used another indexer and it ended up generating a 1GB DB table.


  • Hi Misha

    Thank you for the great plugin, intuitive tutorial, all the behind the scenes help and efficient service. My client is very happy which makes me very happy :) Great job!


    • Hi again Daryl,

      I’m glad to help and I’m glad that everyone is happy :)

  • Hello Misha,

    Thanks for the superb post !!!

    I need your one assistance; I have a querry regarding the multisite widget… Please review my stackoverflow link.

    Thank you.

    • MishaAuthor


      Could you please give me an example how the widgets should like like?

  • Hi Misha, I am new on WordPress. I a set up a multisite using human pro as theme. The top bar menu has a search button which I guess uses the WordPress native search tool. My multisite will have several subsites. Can I use your plugin to search through all of them using the search button provided by the theme? If so, is it easy to set up or will I have to copy paste code in sensible files? Thanks you.

    • Hi Juan,

      I do not know your level of knowledge, so I’m not sure if it will be easy for you or not. But you can always contact me by email and I will try to help you with set up.

  • Hello, is it possible search by multisite page with this plugin? It shoult be work as search by category, but instead of the category will be multisite page. Thank you very much

Comments are closed.
If you need my personal help, please contact me. Contact me