Add Categories filter to Search Unleashed
-
I am posting this trick to add category filtering to Search Unleashed for the MySQL fulltext search since after looking all over the place I couldn’t find it and then decided to dig into the code and implement it myself.
Turns out it’s really easy to add and I believe quite useful.The goal:
If the search provides a cat=1,2,3 argument along with the search terms, then the engine only returns posts where the search terms were found AND belonging to ANY of the categories 1, 2 OR 3.Ex:
If the search URL is: https://myblog.org?s=wordpress&cat=1,2
Then it should display posts where “wordpress” was found (according to the standard Search Unleashed search) AND that were tagged with categories 1 OR 2.An important note:
This hack was implemented and works on wordpress 2.7.1. It is highly dependent on the WP database schema so it might not work on other versions. After searching a bit, it seems that the schema (at least regarding posts and categories relationships) has not changed in WP3, so this should be up-to-date.
However if understood, it should be easy to adapt it for another schema.The hack now:
In plugins/search-unleashed/mysql.php:Add the following lines :
global $wp_query; $cats = $wp_query->get('cat'); if($cats) $sql .= " LEFT JOIN wp_term_relationships ON wp_posts.ID = wp_term_relationships.object_id ";
BEFORE line
$sql .= " WHERE 1=1 "
Then, add the following lines:
if($cats) $sql .= " AND wp_term_relationships.term_taxonomy_id IN ($cats) ";
AFTER line
$sql .= " WHERE 1=1 "
That’s it! Hope this will be usefull to some people!
Cheers!
- The topic ‘Add Categories filter to Search Unleashed’ is closed to new replies.