Exclude terms from search results

Forums › Forums › Search & Filter Pro › Exclude terms from search results

Tagged: 

Viewing 4 posts – 1 through 4 (of 4 total)
  • Anonymous
    #184033

    Is it possible to exclude specific terms from search results?

    For example If I search for the term “Audi” all posts with the term “audio” are returned. Similarly searching for “Ford” will return “affordable”.

    Trevor
    #184042

    If you are using the text search field, this level of sophistication is not built in to the standard WordPress search, which is what our plugin uses for that particular field.

    Anonymous
    #184052

    Bugger.

    I think stack overflow has got me 90% of the way there though. with the following:

    add_filter('posts_search', 'my_search_is_exact', 20, 2);
    function my_search_is_exact($search, $wp_query){
    
        global $wpdb;
    
        if(empty($search))
            return $search;
    
        $q = $wp_query->query_vars;
        $n = !empty($q['exact']) ? '' : '%';
    
        $search = $searchand = '';
    
        foreach((array)$q['search_terms'] as $term) :
    
            $term = esc_sql(like_escape($term));
    
            $search.= "{$searchand}($wpdb->posts.post_title REGEXP '[[:<:]]{$term}[[:>:]]') OR ($wpdb->posts.post_content REGEXP '[[:<:]]{$term}[[:>:]]')";
    
            $searchand = ' AND ';
    
        endforeach;
    
        if(!empty($search)) :
            $search = " AND ({$search}) ";
            if(!is_user_logged_in())
                $search .= " AND ($wpdb->posts.post_password = '') ";
        endif;
    
        return $search;
    
    }
    Trevor
    #184054

    Does that work for you, or not (if not, what issues)?

Viewing 4 posts – 1 through 4 (of 4 total)
Search & Filter Support
Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.

We also use cookies to store items in your cart as well as allowing your to login on the site.

You can adjust all of your cookie settings by navigating the tabs on the left hand side.

By continuing to use this site, you also agree to our Privacy Policy.