milosearch1

The search page in a WordPress blog is normally a bit of an after-thought. I know that until I replaced the WordPress page here with a Google search page, my search results were just like any other category page.

You can do a lot to improve your search results page though. In this post, milo has shown us some of her best techniques for creating a far more useful page.

We will start by adding a counter for the number of results found, then we will highlight the search times in the results themselves, and finally, show you how you can exclude certain categories from your results.

1 – Show the Number of Results Found

The typical WordPress search results page has the title “Search Results.” It’s not very helpful, but we can make it much more dynamic, very easily.

Let’s change our title to something like “Search Result for search terms – 12 Articles

Much nicer, right?

Open your search template file, search.php. In it, search for the following:

1
<h2 class="pagetitle">Search Results</h2>

Now replace this with:

1
<h2 class="pagetitle">Search Result for <?php /* Search Count */ $allsearch = &new WP_Query("s=$s&showposts=-1"); $key = wp_specialchars($s, 1); $count = $allsearch->post_count; _e(''); _e('<span class="search-terms">'); echo $key; _e('</span>'); _e(' &mdash; '); echo $count . ' '; _e('articles'); wp_reset_query(); ?></h2>

And that’s you done! You can use CSS to style individual parts of the title however you like. E.g. The HTML for the search terms will look something like this:

1
<span class="search-terms">search terms</span>

2 – Highlight the Search Terms in Results

When you search on Google or any other major search engine, the words you searched for are highlighted in the results to make it easier for you to scan. We can do the same with your search results page.

In search.php, search for the following:

1
<?php the_title(); ?>

This is the code for showing the title of each of the results. In the default theme, it will be wrapped up in a line like this:

1
<h3 id="post-<?php the_ID(); ?>"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h3>

Now, replace the part with:

1
<?php echo $title; ?>

Finally, before this line, paste the following:

1
<?php $title = get_the_title(); $keys= explode(" ",$s); $title = preg_replace('/('.implode('|', $keys) .')/iu', '<strong class="search-excerpt">\0</strong>', $title); ?>

Your search terms will now be wrapped in tags. Bolding works great for this, but if your headline is already in bold (or h1 etc. tag) then use CSS to style it in some other way for emphasis.

3 – Exclude Certain Categories from Being Searched

You may not want all of your categories to appear in your search results. For example, if you’ve set up a sidebar asides category, you may not want those short snippets tangling up with your results.

To remove a category, open up your theme’s functions.php file (Or create a blank file with that name if your theme doesn’t have one), and paste the following code at the end:

1
2
3
4
5
6
7
8
9
<?php
function SearchFilter($query) {
if ($query->is_search) { 
$query->set('cat','8,15'); 
}
return $query; 
}
add_filter('pre_get_posts','SearchFilter');
?>

Simply change the “8,15″ part to a comma-separated list of the category IDs for any category you want to exclude (Can get the category IDs from WordPress by going to Posts > Categories.

With those 3 steps, we now have a much more dynamic and helpful search page to give to our users. What do you think of them? Is there anything else that you like to see on a search page?

milo WordPress Hacks Series: The code in these posts was written entirely by Milo, a web designer from Oxford, Britain. Check out her website for free WordPress themes and her blog, or follow her on Twitter!

Enjoy this post? You should follow me on Twitter!