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, 2015  /   36

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

once a week, no spam

Comments 36

  • 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
    nail_salons
    Este

    It is likely to be supported by custom fields below.
    https://rudrastyh.com/wordpress/ajax-post-filters.htm

    • MishaAuthorAugust 21, 2016 at 15:08

      Hello,

      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?

        /M

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

        /M

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

  • How come you don’t include a separate search form with this plugin in the widgets area.

    • Hmm, thank you for the idea. I will add it in the next plugin version.

      • That will make it easier because when I search with my form following your instructions (probably my fault) I always get no results found. Plus when I update any post or page with your plugin active it gives me the following error code:

        Warning: Invalid argument supplied for foreach() in /home/synthorg/public_html/wp-content/plugins/true-multisite-indexer/classes/class.model.php on line 413

        Warning: Invalid argument supplied for foreach() in /home/synthorg/public_html/wp-content/plugins/true-multisite-indexer/classes/class.model.php on line 413

        Warning: Invalid argument supplied for foreach() in /home/synthorg/public_html/wp-content/plugins/true-multisite-indexer/classes/class.model.php on line 413

        Warning: Invalid argument supplied for foreach() in /home/synthorg/public_html/wp-content/plugins/true-multisite-indexer/classes/class.model.php on line 413

        Warning: Invalid argument supplied for foreach() in /home/synthorg/public_html/wp-content/plugins/true-multisite-indexer/classes/class.model.php on line 413

        Warning: Invalid argument supplied for foreach() in /home/synthorg/public_html/wp-content/plugins/true-multisite-indexer/classes/class.model.php on line 413

        Warning: Invalid argument supplied for foreach() in /home/synthorg/public_html/wp-content/plugins/true-multisite-indexer/classes/class.model.php on line 413

        Warning: Invalid argument supplied for foreach() in /home/synthorg/public_html/wp-content/plugins/true-multisite-indexer/classes/class.model.php on line 413

        Warning: Invalid argument supplied for foreach() in /home/synthorg/public_html/wp-content/plugins/true-multisite-indexer/classes/class.model.php on line 413

        Warning: Invalid argument supplied for foreach() in /home/synthorg/public_html/wp-content/plugins/true-multisite-indexer/classes/class.model.php on line 413

        Warning: Invalid argument supplied for foreach() in /home/synthorg/public_html/wp-content/plugins/true-multisite-indexer/classes/class.model.php on line 413

        Warning: Invalid argument supplied for foreach() in /home/synthorg/public_html/wp-content/plugins/true-multisite-indexer/classes/class.model.php on line 413

        Warning: Invalid argument supplied for foreach() in /home/synthorg/public_html/wp-content/plugins/true-multisite-indexer/classes/class.model.php on line 413

        Warning: Invalid argument supplied for foreach() in /home/synthorg/public_html/wp-content/plugins/true-multisite-indexer/classes/class.model.php on line 413

        Warning: Invalid argument supplied for foreach() in /home/synthorg/public_html/wp-content/plugins/true-multisite-indexer/classes/class.model.php on line 413

        Warning: Invalid argument supplied for foreach() in /home/synthorg/public_html/wp-content/plugins/true-multisite-indexer/classes/class.model.php on line 413

        Warning: Invalid argument supplied for foreach() in /home/synthorg/public_html/wp-content/plugins/true-multisite-indexer/classes/class.model.php on line 413

        Warning: Invalid argument supplied for foreach() in /home/synthorg/public_html/wp-content/plugins/true-multisite-indexer/classes/class.model.php on line 413

        Warning: Invalid argument supplied for foreach() in /home/synthorg/public_html/wp-content/plugins/true-multisite-indexer/classes/class.model.php on line 413

        Warning: Invalid argument supplied for foreach() in /home/synthorg/public_html/wp-content/plugins/true-multisite-indexer/classes/class.model.php on line 413

        Warning: Invalid argument supplied for foreach() in /home/synthorg/public_html/wp-content/plugins/true-multisite-indexer/classes/class.model.php on line 413

        Warning: Invalid argument supplied for foreach() in /home/synthorg/public_html/wp-content/plugins/true-multisite-indexer/classes/class.model.php on line 413

        Warning: Cannot modify header information – headers already sent by (output started at /home/synthorg/public_html/wp-content/plugins/true-multisite-indexer/classes/class.model.php:413) in /home/synthorg/public_html/wp-includes/pluggable.php on line 1174

        The following plugin used to work well without any modification to theme files (until it became old by not being supported) https://agora.grial.eu/pfcgrial/multisite-search/

        I left you a support message with my problem. Hope to hear from you soon.

  • I never received it. I’ll send another one pertaining to this post.

  • Hi,
    can I use this plugin for a marketplace scheme on Multisite?
    I’d use it to find products across all the subsites/subshops of the sellers.
    There’s a thing I don’t understand, probably due to my english: there are usually two ways to search for something in a website:
    1) by a search box
    2) by categories/ or titles and so on, which are pre filled, so the user has just to follow that route.
    Is there a way to make the plugin works in this 2nd method I’ve mentioned? maybe including the function on each category box/button?
    Hope in your answer
    Thanks in advance

    • Hi Frans,

      yes, it is possible. You can search for products by their titles / content (works like default WP search) or you can search for categories as well by titles or descriptions.

      Anyway, if you have any problems, I can help you with the code.

  • Thanks for your reply Misha,

    just cause I’m a bit slow on understanding something that I don’t master at all: when I say to search by titles, or categories I mean if would be possible to create a menu on the directory page where those categories voices are placed, and the users have just to (click) on a specific category to obain the results,
    instead of (typing) the name of that category into the search field.

    This is the difference I was talking about:
    searching by:
    1) typing the term we want, into the search field
    2) clicking on the term, meaning the category we’ve created in the menu on the main directory.

    Thanks again

    • Yes, both of options can be implemented. If you know how to work with basic WordPress functions, this will be easy for you.

  • Hi,

    Got the plugin and full search working great, but seeing errors on the server,

    PHP message: PHP Warning:  getmypid() has been disabled for security reasons in /www/wp-content/plugins/true-multisite-indexer/classes/class.model.php on line 701

    Any ideas or seen this before? Getting hundreds of these errors every day, even though the actual search is working.

    • MishaAuthorJanuary 7, 2017 at 00:01

      Hi Phil,

      it looks like function getmypid() is disabled on your server.

      Function getmypid() returns ID of the PHP process for the plugin log, I recommend you to clarify this moment with your hosting support provider, but I suppose you could also try to replace this function with time() (just in the code on line 701 in class.model.php replace it).

      Please let me know if you need any assistance with that.

  • Trevor MillerApril 9, 2017 at 04:04

    Is it possible to clear the index and re-index everything? I’m getting stale perma-links.

    • MishaAuthorApril 9, 2017 at 13:04

      Yes, in plugin settings go to the Rebuild Index tab.

    • MishaAuthorApril 9, 2017 at 22:04

      Sorry for delay in replying,

      I made some tests and for me everything is OK. When I update a post link or when I change website permalink structure in settings, the index updates immediately. Even no rebuild index required.

      P.S. I can not find your name / email in orders.

      • Thanks for reply. Manuel Stavropolous purchased the plugin, he is the project manager and I am developer.

      • Could you elaborate on this error message? Does the plugin use the Linux cron or the WordPress cron?

        “If you need old posts to be indexed, you should fix the Cron scheduler on your server — please contact your server technical support — they should help with this.”

        • MishaAuthorApril 10, 2017 at 12:04

          WordPress Cron.

          Yes, this message means that first of all you have to fix WP_Cron. You can check it just by publishing a simple scheduled post. And please, let me know about the results, because I plan to write a complete tutorial about fixing the server WP_Cron problems.

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

Leave your question or feedback

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