Searching a WordPress Blog

12th of April, 2006 (Last modified: 17th of April, 2006) Håvard WWW , Site specifics , WordPress ,

When searching a WordPress blog the pages and the comments are not included. What good is a search engine then, if it doesn't include the site in its entirety?

After searching around I found some plugins that allow the user to search through the pages as well, one plugin even included the comments. However, they are all treated as one single result without any distinction between what's a page, a comment or a post.

What I ended up doing was to create my own search.php and modify it quite a bit from the original one.

The code in its entirety can be seen at http://hvassing.com/upload/search.phps (with nice colour coding) or at http://hvassing.com/upload/search.txt for those who doesn't like colours.

You can try out the script at http://hvassing.com/search/, however, a note should be made about the JavaScript; it is still in development and is not included in the two above mentioned files. If you're interested in it, getting hold of the code should be easy enough. The JavaScript simply provides a iTunes-like way of searching through the search results. Note that the search page on this site does not validate as it uses some Safari-only attributes and inline CSS.

The bit that is the work horse is the one that fetches all the required information and passes the information on to $pages which is then looped through using a foreach-loop. After the foreach-loop all the variables fetched from the database can be accessed and used either for outputting or further manipulation. The beauty of it is that it will only output the search results if there are any, however there's a huge potential for improvement. The drawback is that everything on your site, regardless of age and intended audience will be easier to get hold of, but then again; that's the beauty of the web.

Any comments and suggestions are greatly appreciated.

Update:
Due to the way WordPress eat my code I've now separated the code to a separate text-document and a phps-document.
My apologies for the bad formatting of code, working on fixing it.


2 Responses to “Searching a WordPress Blog”

  1. This is great! Always felt that the WP search is somewhat lacking. Maybe you could get this into the official package? :)
    Nice blog you’ve got here.

    Føles rart å skrive på engelsk til en nordmann forresten ;)

    1
  1. 1 » Code Highlighter Plugin » Hvassing.com