How to prevent Search Everything from returning duplicate hits with WordPress 2.1

Posted on February 19th, 2007 by Reiner.
Categories: English, Computers.

After upgrading to WordPress 2.1 Search Everything often returns the same pages multiple times. 

22-Feb-07 Update: The information below has been superceeded, as Dan has just released version 3.01 of the plugin that includes code to prevent duplicate hits.

One of a my friends runs a property management agency, where WordPress is used as an internal knowledge base to document internal procedures and the like. Quite often, valuable information is not contained within the main post, but added later as a comment. Search Everything makes sure that this information can be searched and retrieved.

After upgrading to WordPress 2.1 and installing Search Everything Version 2.6, searches will quite often return the very same pages multiple times.

I had a look at both Search Everything and WordPress Code and found that WordPress 2.1 does not use the distinct option when querying the databse, but provides a filter instead that may, at its discretion, return the magic ‘DISTINCT’ to cancel duplicate entries. The distinct option may need additional resources within the database, so I believe, it’s off by default for performance reasons.

Here’s my fix (the bolded lines below), which will cause WordPress to cancel duplicate results. It should be applied to the search_everything2.php file and requires WordPress 2.1+ to work (but won’t harm older WordPress releases) :

if ("true" == get_option('SE2_use_metadata_search')) {
  add_filter('posts_where', 'SE2_search_metadata');
  add_filter('posts_join', 'SE2_search_metadata_join');
  SE2_log("searching metadata");
  }            

// http://blog.saddey.net/?p=167
add_filter(’posts_distinct’, SE2_search_distinct);              

// http://blog.saddey.net/?p=167
function SE2_search_distinct($distinct) {
  if($distinct == ”) {
    return ‘DISTINCT’;
  }
  return $distinct;
}              

//search pages
function SE2_search_pages($where) {
  global $wp_query;

The fix is very basic and may apply the distinct clause, even when it might not be required as dictated by your Search Everything options. But I don’t think, there’ll be any problem with this and would like to leave the final inclusion to the author of Search Everything. Thanks to Dan Cameron for this great plugin! 

After having implemented the fix, I found, that the concept for the fix has already been outlined at WordPress Hackers - Getting double/triple posts listed on some blogs ten days ago.

Aditya has posted a fix as well, which was taken from Google Code, but I believe that my version more closely adheres to WordPress 2.1 standards (the ‘posts_distinct’ just returns a keyword for the tasks it’s supposed to instead of modifying the whole query).

1 comment.

Dan

Comment on February 22nd, 2007.

I just release 3.0 with this fix. Thanks.

Leave a comment

Comments can contain some xhtml. Names and emails are required (emails aren't displayed), url's are optional.