WordPress.org

Ready to get started?Download WordPress

Forums

Search Unleashed
[resolved] Add Categories filter to Search Unleashed (3 posts)

  1. Renuplex
    Member
    Posted 3 years ago #

    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: http://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!

    http://wordpress.org/extend/plugins/search-unleashed/

  2. liamk23
    Member
    Posted 3 years ago #

    Hi, Could anyone advise as to how I send data from a form containing multiple dropdowns as cat=1,2,3,4 etc

    So, I require a form which has say 4 dropdowns selecting 4 seperate categories, when selected I want the form to send cat=1,2,3,4. I have the backend working its just getting the form to send the data in the correct form.

    At the moment it sends: http://domain.com/index.php?s=searchterm&dropdown1=1&dropdown2=2&dropdown3=3&dropdown4=4 - i would want this to read s=searchterm&cat=1,2,3,4

    Thank you very much for any help anyone can offer.
    Regards, Liam

  3. xer0x
    Member
    Posted 3 years ago #

    Thanks Renuplex! I've been googling all over for this. It surprises me that this functionality isn't already built into a handy plugin like Search Unleashed.

    I got your code working on my site with a couple tweaks. I'm posting them here just in case anyone else is looking for this too.

    Thanks again,

    global $wp_query;
    $cats = $wp_query->get('cat');
    if($cats) {
        $sql .= " LEFT JOIN {$wpdb->prefix}term_relationships ON {$wpdb->posts}.ID = {$wpdb->prefix}term_relationships.object_id ";
        $sql .= " JOIN {$wpdb->prefix}term_taxonomy ON {$wpdb->prefix}term_relationships.term_taxonomy_id = {$wpdb->prefix}term_taxonomy.term_taxonomy_id";
    }
    $sql .= ' WHERE 1=1 ';
    if ($cats) $sql .= " AND {$wpdb->prefix}term_taxonomy.term_id IN ($cats) ";

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic