How to Use Global Search in WordPress Multisite
In this step-by-step tutorial, I will guide you through the process of creating a search engine that allows you to search across all blogs within your WordPress multisite network.
The methods described here will also work great for WooCommerce products.
The only thing that I need you to keep in mind is that you’re really using a WordPress multisite network; if you have just a network of standalone WordPress installs, it is not what we need.
This method is also best suited for Classic and Hybrid WordPress themes, not for the Block themes (yet). If you need it to be adopted for a block theme, let me know in the comments below.
Also, please keep in mind that the Network_Query class is not available in WordPress Multisite by default; you need to install my plugin if you want to use it.
A Simple Example of Implementing the Global Search
Let’s start with the code snippet, which you can use wherever you want to display the search result:
/**
* @snippet A global search example
* @author Misha Rudrastyh
* @url https://rudrastyh.com/wordpress/search-across-wp-multisite.html#example
*/
$args = array(
's' => 'wordcamp asia', // the search query is here
'posts_per_page' => -1
);
$query_search = new Network_Query( $args );
if( $query_search->have_posts() ) :
while( $query_search->have_posts() ) : $query_search->the_post();
echo '<h2>' . $query_search->post->post_title . '</h2>';
endwhile;
else :
echo '<p>Nothing found for your search criteria.</p>';
endif;As I already mentioned before, the Network_Query class is a part of my multisite search plugin; it is similar to the original WP_Query class and accepts all the same parameters, but works for all blogs in your multisite network.
If this example is not crystal clear for you, let’s continue to a step-by-step global search engine implementation below.
Creating a Global Search Results Page
In this part of the tutorial, we will work specifically with a PHP template for our WordPress theme. Since it is a PHP template, we can say that we’re working with a classic or hybrid theme here.
1. Creating a WordPress page template where we display global search results
The long story short, you have to open your current theme folder (it would be better if you’re using a WordPress child theme) and create a file with a custom name there.

search.php, searchform.php; and it would be even better if you place your file inside page-templates folder.Once you’ve created your file, please define it as a page template. To do that, open your file and add the piece of code below at the beginning.
<?php
/*
* Template name: Global Search
*/The first step is almost done! Now let’s create a Page in WordPress admin, Pages > Add new, and select this new template there.

2. Displaying global search results
In this example, we will add the code from the first example in this tutorial to our page template code.
<?php
/*
* Template name: Global Search
*/
get_header();
$args = array(
's' => 'wordcamp asia', // the search query is here
'posts_per_page' => -1,
);
$query_search = new Network_Query( $args );
if( $query_search->have_posts() ) :
while( $query_search->have_posts() ) : $query_search->the_post();
echo '<h2>' . $query_search->post->post_title . '</h2>';
endwhile;
else :
echo '<p>Nothing found for your search criteria.</p>';
endif;
get_footer();The example is still over-simplified:
- It is better to use
the_title()orget_the_title()to display post titles, but here, I am using it straight from theWP_Postobject, which is not perfect, to say the least. We must have at least theesc_html()function, but we will come back to it a little bit later. - I have also added
get_header()andget_footer()functions here. - As you can see, I completely skipped the pagination part by adding
posts_per_page => -1, but you can read more about it in a separate tutorial.
Let’s continue reading and improving our WordPress Multisite global search functionality.
3. Creating a search form
Ok, we have the search results page, but we can see that the page itself is kind of static and connected to a static search query.
To fix that, we need to create a search form, where its action attribute should point to our global search results page.
<form action="<?php echo site_url( 'network-search' ) ?>" method="GET">
<input type="text" name="q" />
<button>Search</button>
</form>The next thing is to modify the $args array of Network_Query.
$search_query = ! empty( $_GET[ 'q' ] ) ? $_GET[ 'q' ] : '';
$args = array(
's' => $search_query,
'posts_per_page' => -1
);The complete code will look like this:
<?php
/*
* Template name: Global Search
*/
get_header();
$search_query = ! empty( $_GET[ 'q' ] ) ? $_GET[ 'q' ] : '';
?>
<form action="<?php echo site_url( 'network-search' ) ?>" method="GET">
<input type="text" name="q" value="<?php echo esc_attr( $search_query ) ?>" />
<button>Search</button>
</form>
<?php
$args = array(
's' => $search_query,
'posts_per_page' => -1
);
$query_search = new Network_Query( $args );
if( $query_search->have_posts() ) :
while( $query_search->have_posts() ) : $query_search->the_post();
echo '<h2>' . $query_search->post->post_title . '</h2>';
endwhile;
else :
echo '<p>Nothing found for your search criteria.</p>';
endif;
get_footer();You can also check if the search query is provided; otherwise, the Network_Query will return just the latest posts:
if( $search_query ) {4. Displaying posts’ data (how to get links to posts, display post thumbnails, etc.)
As you can see in the previous example, we displayed only post titles, and not in 100% correct way, but it is also possible to display… well.. anything you want 🙂 You can use regular WordPress functions for this.
Let’s try to display permalinks, post images, and post dates.
while( $query_search->have_posts() ) : $query_search->the_post();
// we need to switch to a specific blog in order WordPress functions to work
switch_to_blog( $query_search->post->BLOG_ID );
// post titles
echo '<h2>' . get_the_title( $query_search->post->ID ) . '</h2>';
// post thumbnails
echo get_the_post_thumbnail( $query_search->post->ID, 'medium' );
// post date
echo '<time>' . get_the_time( 'j F, Y', $query_search->post->ID ) . '</time>';
// permalinks
echo '<a href="' . get_permalink( $query_search->post->ID ) . '">Read more</a>';
restore_current_blog();
endwhile;The long story short, here is what we’re doing in the example above:
- When we start to display the body of a post, we need to switch to the subsite this post belongs to by using
switch_to_blog(). It is not ideal to do it repeatedly a lot of times (if you have a lot of posts in your search results); in that case, you can read about the performance. - After that, we can use any standard WordPress loop functions,
get_the_title(),get_the_post_thumbnail(),get_the_time(),get_permalink(), etc., the only requirement is that the function must accept a post ID as an argument.
That’s it, our WordPress multisite search engine is ready!
Search Posts by Their Custom Field Values
The thing is that sometimes we need to search posts globally in a multisite network; however, not in a standard way, but using custom field values. Sure thing, my Network_Query plugin allows you to do that.
Probably, you’re already familiar with the meta_query argument, below is an example:
$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' => 'london'
'compare' => 'LIKE'
)
)
);
$query_search = new Network_Query( $args );Search Terms by Part of Their Titles and Descriptions
The plugin allows you to search not only posts (and custom post types) globally, but also categories, tags, and custom taxonomy terms. It can be achieved with a built-in function network_get_terms(); by the way, you can read more about it here.
Below are the examples:
// 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;
}
}If you have any questions about the global search implementation, feel free to ask in the comments section below.
Misha Rudrastyh
Hey guys and welcome to my website. For more than 15 years I've been doing my best to share with you some superb WordPress guides and tips for free.
Need some developer help? Contact me
I wanted to perform a search of the following multi-site. It is likely to be supported by custom fields below.
Hello,
First of all — I do not recommend to use
pre_get_postsfilter 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_QuerytoNetwork_Queryandwp_reset_postdata()tonetwork_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
Hi Malin,
yes, it is possible,
my plugin doesn’t change standart WordPress queries.
Currently there is no demo page.
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
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?
Hi Ramon,
yes, you can do it with my plugin.
I never received it. I’ll send another one pertaining to this post.
I’ve sent the mail puplicates to it.
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 PHP warnings on the server,
PHP message: PHP Warning: getmypid() has been disabled for security reasons in...Any ideas or seen this before? Even though the actual search is working.
Hi Phil,
it looks like function
getmypid()is disabled on your server. Please contact your hosting provider to clarify that moment.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 withtime()(just in the code on line 701 inclass.model.phpreplace it).Please let me know if you need any assistance with that.
A small update, I decided to remove the
getmypid()function from the plugin, since it is not really needed there.Is it possible to clear the index and re-index everything? I’m getting stale perma-links.
Yes, in plugin settings go to the Rebuild Index tab.
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! :)Hi Daniel,
I am glad you’ve figured it out! 🙂
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!
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!
Hi, yes, you can exclude posts based on a custom field value with
meta_queryparameter 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,
yes, it does.
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
Hi again Daryl,
I’m glad to help and I’m glad that everyone is happy :)
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
Hello Milan,
If you mean a post type “page” – yes, it is possible.
Hi! Does your plugin works with sphinx?
Hi Jackky,
I didn’t test it with it
I think you should test and improve your plugin with this feature. Because searching across all network without sphinx seems to be a little bit useless.
Thank you for your suggestion 👍
Downloaded the plugin with no problem and worked straight away. I however needed some customisation work doing on the search query. Misha worked on my request in super fast time and it now does exactly what i needed it to do. With the coding being done i can now adjust to to search for other details if i need to.
Many thanks
Great product
Patrick
Hello Misha,
Does your plugin support the “episodes” post type?
Thank you,
Chris
Hi Chris,
Yes, why not
The plugin looks like it will do most of what I need pretty easily, but have a couple questions before purchasing it.
1) Does this plugin verify user permissions before including the results? For example, if I have some pages that are restricted for only certain users, will that page:
– show in the results if I am one of the allowed users
– be hidden from results if I am not one of the allowed users
2) Can we show the name and link of the network site in the results? I assume this is possible using the switch_to_blog method?
Hey Scott,
1) It depends. If you’ve added some restrictions via the code or another plugin, you need to check the way it is added (what hook is used, etc).
2) Yes, of course. You can either use a
BLOG_IDproperty orswitch_to_blog()(it will be simpler), yes.OK, I haven’t yet determine how restrictions would be set, but am assuming at this point that it would be based on whether that user was a member of the site where the page was created.
– Most sites on the network will be available to all users.
– A few department sites will be restricted to people in the specific department.
– It is unlikely that two different pages in the same site would have different permissions, so it could be based on site membership, and not using any plugin.
If that was the case, would I need to manually filter the query results, or will the network query handle that?
Thanks
Hey Scott,
I think, in this case, you don’t even need to filter the query, you only need to use the
blog_idparameter and pass the blogs there where the users are added as members. Example of using this parameter.How to get all blogs a user is added to? The standard WordPress function
get_blogs_of_user()should do the trick, I think.Thanks!