After the WordPress 3.0 release, a sought after solution have been a WordPress multisite search function across all blogs.

A recent project required just such a solution, and although there were some plugins already released both free or premium, they each had flaws in one way or another for the specific project.

The following solution is perfect for small WordPress multisite solutions, but make sure to read on after the code for a few comments about this solution.

WordPress Multisite Search

To use the code, edit or create the search.php file in your current theme, and insert the following code:

$searchfor = get_search_query(); // Get the search query for display in a headline
<h1>Search results for '<?php echo $searchfor; ?>'</h1>

<?php $query_string=esc_attr($query_string); // Escaping search queries to eliminate potential MySQL-injections 
$blogs = get_blog_list( 0,'all' ); 
foreach ( $blogs as $blog ): switch_to_blog($blog['blog_id']); 
	$search = new WP_Query($query_string); 
	if ($search->found_posts>0) {
		foreach ( $search->posts as $post ) {
			$author_data = get_userdata(get_the_author_meta('ID'));
			<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>

				<span><?php the_time('Y/m/d') ?></span>
				By <?php the_author_posts_link();?> 

				<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>

				<div id="entry-content"><?php the_excerpt(); ?>

restore_current_blog(); // Reset settings to the current blog

The code is very simple, but can quickly become very resource demanding. The script runs through each subsite on your WordPress multisite blog, and displays the results in the layout you wish.

The code above uses the function get_blog_list() which has been deprecated, but at the time of writing this there is no alternative function provided.
The search results are listed in the order found, meaning the first blogs will be displayed first.
The search results are not sorted by relevance.
There is no caching of search results.
The solution is best for small WordPress multisite installations.

[box type=”info”]

Note: Christopher Ross from The Helpful Coder has created a function to replace get_blog_list() in his post How to List all the Sites Hosted on a Multisite Install of WordPress 3.0.

I have not used his function in the example code above.