Advanced Contextual search for WordPress

June 7th, 2004
Weblog Add-Ons, WordPress Hack

Anne van Kesteren gave me some code to make intelligent searches inside WordPress and I wrapped it up in a nice install tutorial. Some of the features of this hack includes
-text based “fuzzy” searches within WordPress
-a nice Google like interface which is very customizeable
-search results with decreasing relevance to search topic
-trap for words which are less than four letters long, the search then gets routed through the default WP search
-“Next results” button which shows the next 10 (or less) results in relevance
-very simple installation with no effect on future upgrades

Test install is on this blog. Simply search for something from the form on the right.

Please post suggestions/bugs.
Download the zip file here:




  1. Mark says:

    Did you run search_upgrade.php that is included in the zip file?

  2. dodo (1 comments.) says:

    the script is great. but it doesn’t highlight the search keywords in the results?

  3. Ryan says:

    Just installed the search engine. I get this error:

    Database error: [Can’t find FULLTEXT index matching the column list]
    SELECT ID,post_title,post_content,post_excerpt,post_date, MATCH(post_title,post_content) AGAINST (‘united forest’) AS score FROM wp_posts WHERE MATCH (post_title,post_content) AGAINST (‘united forest’) LIMIT 0,11

    No results.

  4. Mark says:

    There is no search word highlighting because words are not searched for as a whole, there is some fuzzy matching going on.

  5. Clay Heaton (1 comments.) says:

    Does this search hack search the text of the excerpt, or just the main contents of the post?

  6. Mark says:

    Right now it only works with the post title and post content and not the excerpt.

  7. BB (5 comments.) says:

    I wrapped my site design around the code and made a new search.php file. The search results don’t span from the left to the right within the div, as I would like them to. Complete newbie question:

    How do I get the search results to go completely across the center content div?

    FYI: You should change line 149 of search.php from “search-beta.php” to “search.php”.

  8. Mark (34 comments.) says:

    I will make that change.
    As for the CSS, take a look at the source of my search page (or any single post page). The menu is suppressed.

  9. BB (5 comments.) says:

    Yes, I thought I had that, too. Take a look at this page on my site. You can see how the content isn’t filling up the center div. What did I do wrong? You can view the code I have in the search.php file here.

  10. BB (5 comments.) says:

    Apparently, the href’s got stripped out of that last comment. Here they are:

  11. Kelso (2 comments.) says:

    BTW – don’t hate me ’cause I use tables :P
    And, is there any reason why it wont show any results if I search “Nike”, but it will if I search “Air” keeping in mind that I have to posts with the words “Nike Air” in their titles… ?

  12. Kelso (2 comments.) says:

    This plugin is great :)

    However, I might be asking much, but is there ANY way to include PostMeta tags in the search page?
    I’ve sort of included them, but the values don’t “spit out”
    (that page is still much under construction) ;)

  13. Jeffrey Morgenthaler (1 comments.) says:

    I’ve used this before, and it worked great, but this time something’s wrong. I get no results for searches over three characters – four or more, it tells me I have no search results. COuld this be a result of using WordPress 1.2.1?


  14. Mark says:

    I am not sure where it is breaking because your site seems to be searching just fine.

  15. Mark says:

    Thisis a test

  16. Andrea says:

    I just tried out this search on a local install of WP 1.2.2, but decided that it wasn’t what I want and went back to using the normal search (i.e., I changed the searchform action back to the default and deleted the files from my directory).

    I was able to create a new post after the change, but it wouldn’t let me edit it (changes wouldn’t be saved), and when I tried to edit another post, I got the following error messages on top of my header image:

    Database error: [Can't open file: 'wp_posts.MYD'. (errno: 145)]
    SELECT DISTINCT * FROM wp_posts WHERE 1=1 AND ID = 24 AND post_date_gmt < = '2005-01-14 15:54:59' AND (post_status = "publish") GROUP BY wp_posts.ID ORDER BY post_date DESC Database error: [Can't open file: 'wp_posts.MYD'. (errno: 145)] SELECT DISTINCT YEAR(post_date) AS `year`, MONTH(post_date) AS `month`, count(ID) as posts FROM wp_posts WHERE post_date < '2005-01-14 16:54:26' AND post_status = 'publish' GROUP BY YEAR(post_date), MONTH(post_date) ORDER BY post_date DESC

    and a "Sorry, no posts matched your criteria." message in the content area. Everything worked fine before the search-upgrade, and I really hope you can help me with this.

  17. Mark says:

    Sounds like your system is caching some items. try rebooting the server or restarting.

  18. Andrea says:

    I tried both. Same error message. I *think* I didn’t do anything apart from running the upgrade_search.php, but I can’t rule out mistakes on my part. Any ideas? Don’t know if you need it, but here’s the full error message again, it got truncated in the previous post:

    Database error: [Can't open file: 'wp_posts.MYD'. (errno: 145)]
    SELECT DISTINCT * FROM wp_posts WHERE 1=1 AND post_date_gmt < = '2005-01-14 20:25:59' AND (post_status = "publish") GROUP BY wp_posts.ID ORDER BY post_date DESC LIMIT 1

  19. Andrea says:

    It got truncated again.

  20. Andrea says:

    Okay, I managed to fix the wp_posts table with the help of the MySQL REPAIR TABLE command and everything *seems* to work again now. But it’s doing the thing again where it doesn’t save changes to already-published posts, which has never happened before. I’ll try a restart tomorrow (after some sleep).

    Sorry for spamming your blog, I hope this counts as (welcome) bug-reporting.

  21. essdsm says:

    Nice mod but it’s a bit too rigorous since it displays private, drafts etc I would rather it didn’t. Can you provide and example of how to limit the search to published posts only?

    Many thanks

  22. Mark says:

    Replace line 108 in search.php with the following line. Make sure there are no line breaks, all of this goes on one line.

    $sql = "SELECT ID,post_title,post_content,post_excerpt,post_date, MATCH(post_title,post_content) AGAINST ('".$_GET['q']."') AS score FROM ".$table_prefix."posts WHERE MATCH (post_title,post_content) AGAINST ('".$_GET['q']."') WHERE post_status= 'publish' LIMIT ".$_GET['next_value'].",".(intval($_GET['next_value'])+$max_results+1);

  23. Mark says:

    For the most part any input is really welcome, so no problem. I hope you got your issue fixed.

  24. James (1 comments.) says:

    I wonder why no one knows about WHY THE TABLES CRACH AND WHY THIS ERRNO: 145 COMES UP. We all have been discussing the remedy for the identified problem… we we still don’t know WHAT ACTUALLY BREAKS THESE TABLES TO CAUSE THIS ERRNO 145!

    I’m facing this problem damn frequently…. and so I want to indentify the root cause of this problem…. please anyone let me know if you know….

    Thanks in Advance.

  25. Mark says:

    this probably comes from a corrupted Mysql table and the only answer I have for you is to backup before you install any upgrade/plugin that modifies your tables. In the meantime, might help.

  26. Eve says:

    This is a great improvement over WP’s default search — thank you!

    One question. How do I get the results to sort by date as opposed to relevance?

  27. Mark says:

    It looks really strange on sort by date, but if thats what you want do the following:
    Line 103 in search.php, add the following:
    $sql .= ' ORDER BY post_date ASC ';

    Replace line 108 with the following:

    $sql = "SELECT ID,post_title,post_content,post_excerpt,post_date, MATCH(post_title,post_content) AGAINST ('".$_GET['q']."') AS score FROM ".$table_prefix."posts WHERE MATCH (post_title,post_content) AGAINST ('".$_GET['q']."') ORDER BY post_date DESC LIMIT ".$_GET['next_value'].",".(intval($_GET['next_value'])+$max_results+1);

    Now this is considering you want the date sorted descending, if not replace DESC above with ASC.

  28. Eve says:

    Thank you very much. I am not sure why it “looks really strange” but it works well for our site (a political blog where we want the latest “news” on a given subject available first, or at least make it easier to find older posts).

  29. David Merwin (2 comments.) says:

    I have updated the search plugin to work with 1.5 of wordpress. Could I send it to you and have you announce it?

    you can email me at the above.

  30. Randyd says:


    I’ve just installed your search script using my own style sheet. Doesn’t see anything with my mod rewrite rules of %postname%. When I go back to the default permalink structure on a WP 1.2.2 it works just fine.
    Any ideas?


  31. Sumit says:

    Does anybody know how I can adjust the code so that it will also search through the category titles? Can’t seem to find any plugin that can do this…

  32. Eniko says:

    I tryed to run the search_upgrade.php after copying bith scripts to the root of my blog an it gaves me the following error: No Database Selected. What should I do?


  33. Bhoffmann (1 comments.) says:

    I got the same error.

  34. Joakim says:

    Is it possible to change the URL structure?
    I prefer “” and have a search page under /search/ without any query to link to if I want someone to search.

    I would be very happy if it was possible to translate /search/ to /sok/ (Swedish) in the URL above.

    Thanks for a great “plugin”!


  1. […] for providing the start to my skinning. Thanks to Mark fir the link.I was testing his new search function for WordPress naturally when I fou […]

  2. […] lts in list format with title and blurb, based on a tutorial at noscope, and code from the Weblog Tools Collection. The search feature can b […]

  3. […] — Ajay @ 9:56 pm Been adding a few plugins to WordPress. Just installed the Advanced Contextual Search for WordPre […]

  4. WordPress Hack: Advanced Contextual Search
    Weblog Tools Collection has an advanced contextual search hack which provides an intelligent search engine for your WP weblog. This hack features text based “fuzzy” searches within WordPress, a nice Google like interface which is very customizable,…

  5. […] 7; WordPress Been adding a few plugins to WordPress. Just installed the Advanced Contextual Search for Wor […]

  6. […] ou wish to further improve your search page, you may want to look through the following. * Advanced Contextual search for WordPress * 404 Sea […]

Obviously Powered by WordPress. © 2003-2013

page counter