Improving WordPress Search

WordPress search sucks. It’s well known.

A friend has been looking for what he thinks is the solution and, let’s be honest, he’s got it pretty much right.

He believes any half-decent search should…

  1. return posts by most relevant first
  2. do proper searches based on the phrase you submit
  3. indicate which words matched the post
  4. show how many posts matched
  5. list the posts by just title, or summary or full and allow you to switch
  6. show which page you’re on, if there are more than one page of results

I’m not sure about point 5 – your average search engine, for instance, will give a summary and title and allow you to click it for the full details. I think a search should work like that.

Unfortunately my friend hasn’t found a solution..

I’ve looked at various plugins, but not really had any luck finding one which fixes all the problems.

So, not knowing whether I would be able to solve this dilemma with existing plugins or if I’d end up writing my own, I set about finding an answer.

And I did.

Here’s what you need to do.

Improving the Search Results

Install Search Unleashed. This will add an improved search ability, will show post summaries, highlight searched text and display results in order of relevance.

I also found I had to manually update my themes search.php file to change the rest of the summarised output – removing the calendar image, etc. You don’t have to do this, but it provided a nice compact result.

Show the Number of Results

Install Results Count. You will then need to add some code not only to your search.php theme file but (if you want) to archive.php. This will now display the number of results and which ones you are current viewing. For example, Showing results  1 - 16 of 32 for the search term:  wordpress.

Install WP Page Numbers. Again, you will need to follow the instructions for inserting the appropriate code into search.php. The configuration options will also allow you to change the style of the output. What this plugin does, in a nutshell, is display the current page of results plus lists the other pages available, and let’s you move between them. For example…

Line Numbers Example

Increasing the number of Search Results Per Page

The one problem with summarising your search results is that the blogs are a lot smaller on the page. WordPress only gives you one place to specify the number of posts per page and this applies to all forms of output – this means that the summarised results will take up a lot less of the page and, if there’s a lot of results, you may be aching to increase this number.

In the case of my blog, I have the number of posts set to 5. I found that the number of summarised search results would look best at 16.

Unfortunately, this is a bit of a manual fix. First of all, dig out your themes search.php file again. Hopefully you should be able to find the following line of code within it…

<?php if (have_posts()) : ?>

When you have, insert the following just before it…

<?php
$wp_query->query_vars["posts_per_page"] = 16;
$wp_query->get_posts();
?>

This will override the number of posts just for the search results. This solution doesn’t work for the Results Count plugin though, so you will need to change this too. As it’s a change to the plugin code, be aware that you may need to make this change again if you ever update the plugin.

In results-count.php, find the line…

$numposts = $wp_query->found_posts;

Now, insert after it…

$posts_per_page=16;

The Result

I think that covers pretty much all of what Tony was after. Except point 5.

If you’d like to try the results out then, well, do a search on this site. Feedback is always appreciated!

8 responses

  1. »Lesenswertig« am 07. July 2009 | Denkwertig, der persönliche Blog von René Fischer

    […] Improving WordPress Search Shared um 07:29 Uhr via Delicious […]

  2. THANK YOU SO MUCH! I’ve been looking everywhere for some of these fixes. You are amazing and this page is a god send. The number of search results per page worked like a charm! FYI it also works on archives.php

  3. I’d been looking for an answer on how to increase the search output results, and even got a response from a WP mod – very arrogant.
    But after a little digging, I ended up here, and now my problem has been solved, many thanks.
    Darron.

  4. […] knows it, and so do a lot of other people. Problem is, most of the solutions out there require editing core code and other […]

  5. Nicely done.

    A great little way to get around the failings of WordPress’ search function.

  6. petercwc avatar
    petercwc

    Great details, thanks.

  7. […] I wrote an article some time ago highlighting ways to implement search improvements to WordPress (and the default search DOES suck). Now, I’d like to update you on where I am, including some further changes that I made last night. […]

  8. […] couple of months ago, I talked about improving the search facility built into WordPress. However, soon after, I also mentioned problems […]

Leave a Reply to Improving WordPress Search Further | David ArtissCancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from David Artiss

Subscribe now to keep reading and get access to the full archive.

Continue reading