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).

/50 comments

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:

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

More about WP Multisite:

Need some help with WordPress Multisite?

If you need some professional developer help, I will be happy to assist you.

Contact me Who I am?

Comments — 50

Leave a comment

php js HTML CSS Code