Support Forums

The forums are closed and will be removed when we launch our new site.

Looking for support? You can access the support system via your account.

Daniel Warren

Forum Replies Created

Viewing 1 post (of 1 total)
  • Daniel Warren in reply to:
    Exclude terms from search results
    #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;
    
    }
Viewing 1 post (of 1 total)