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.

You can choose almost any name for you file but please avoid words search.phpsearchform.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.

Here we have default WordPress TwentyTwentyOne theme and a Gutenberg editor deactivated for pages. Depending on a theme you’re using, this page could look a little bit different.

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.

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();

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.

$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;
	}
}