WordPress Multisite Search

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:

<?php
$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 ) {
			setup_postdata($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(); ?>
				</div>


			</div>

			<?php
		}
	}
endforeach;
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.

Read also  Change how the categories are listed for posts

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.

[/box]

7 thoughts on “WordPress Multisite Search”

  1. Christopher Ross (The Helpful Coder)

    Thanks for the mention Lars, you have a great site here.

  2. Not only was this article useful but i love your theme, viva la wordpress!

  3. Laura Gentry

    This is great. I’m using this in tandem with the Search Unleashed plugin to access tags and categories as well. So far, working like a charm. Will be interesting to see how it holds up as more content is added to this network. I’m hoping a good plugin comes along before it reaches critical mass. πŸ™‚

  4. You’ve just saved me several hours of tinkering. I’m doing a corporate site using multi-site that includes the main site and 2 additional blogs. As far as the end user can tell this is all one site so I wanted a more seamless solution that the other plug-ins I’d found. This worked perfectly. Thanks!

  5. downloadtaky

    The Helpful Coder website is Offline πŸ™
    How to search only in a couple of websites instead of all the website under MU installation?
    On my MU installation there are 10 websites but I want to search only in 3 of them how to do it?

  6. thanks for this code. It got me really on the way finding a multisite search functionality.
    Although it works, I ran in a lot of trouble with paging the results and the ‘not found’ messages.
    I found some other plugins, but they all had bugs and issues. Then I found this plugin:

    http://welcome.totheinter.net/wpsearchmu/

    It works with the Lucene search engine, which is very good. It works really well with wordpress 3 and multisite enabled (I use subdirectories). Just follow the basic instructions. You have to install in wp-content> mu-plugins directory and enable the options for each blog. The relevance of the results is much better, paging works and not found messages also.

  7. The link to “How to List all the Sites Hosted on a Multisite Install of WordPress 3.0.” doesn’t work (or exist).

    You can modify the code as this :

    // 1 is the id of the administrator
    $blogs = get_blogs_of_user(1);
            foreach ( $blogs as $blog ):
            switch_to_blog($blog-&gt;userblog_id);
            ...
    

    I’m looking for add pagination to results

Comments are closed.

Free eBook - 

Guide to success with SEO

Join the newsletter for more interesting articles, discount code for SEO Booster Pro and much more.

Guide to success

with SEO 2018

FREE

We respect your privacy and do not share your details with others

Join our newsletter and get a free eBook, interesting articles, discount codes and much more.