How to Search across All Sites in WordPress Multisite Network
Step by step tutorial about creating global search in your Multisite Network.
All the examples below require my plugin to index all the network posts and terms.
Global Search Go-to Example
$args = array(
's' => 'wordcamp london', // 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;
Network_Query
class is the part of my multisite search plugin, it is similar to 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, I recommend you to check my step by step guide below.
Step-by-step Tutorial
1. Create a WordPress Page Template for 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 even would be better if you place your file inside page-templates
folder.Once you’ve created your file, please define it as a page template. In order to do that, open your file and add 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. Print Global Search Results
In this example we combine the code from the first part of this tutorial with our page template code.
<?php
/*
* Template name: Global Search
*/
get_header();
$args = array(
's' => 'wordcamp london', // 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();
I have also added get_header()
and get_footer()
functions here, because they are used in a like 99% of WordPress themes.
Thinking about pagination? I have a tutorial about it.
3. Create a Search Form
Ok, we have search results, but can see that they are quite static and connected to a single query “wordcamp london”.
In order to fix that we create a search form. For 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 $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. How to Get Permalinks, Display Post Thumbnails etc.
As you can see in the previous example, we displayed only post titles, 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 date.
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;
Search Posts by their Custom Field Values
You probably 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' => 'london'
'compare' => 'LIKE'
)
)
);
$query_search = new Network_Query( $args );
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
Hey guys and welcome to my website. For more than 10 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.
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.html
Hello,
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
toNetwork_Query
andwp_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 errors on the server,
Any ideas or seen this before? Getting hundreds of these errors every day, 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.php
replace it).Please let me know if you need any assistance with that.
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_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,
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