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  /  September 2  /   46

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>';
endforeach;

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:

    <?php
    /*
     * 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" />
    	<button>Search</button>
    </form>

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',
		array(
			'key'     => 'key1', // key1 must contain "wordcamp"
			'value'   => 'wordcamp',
			'compare' => 'LIKE'
		),
		array(
			'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>';
endforeach;
network_reset_query();

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 the best of WordPress

Subscribe to this weekly newsletter to receive the latest blog posts by email.I respect your privacy. Your email is safe with me.

Comments 46

← 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?

  • 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 June 1, 2017 at 18:25

      Hello,

      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 June 2, 2017 at 01:01

          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.

    Regards

  • 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!

    Regards
    Daryl

  • Gray Carry July 7, 2017 at 10:51

    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.

    https://wordpress.stackexchange.com/q/270575/121845

    Thank you.

Leave your question or feedback

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