Exclude Specific Term from Search

It is possible to search for posts with a specific term by appending the URL with:

?&term=abc

Is it possible to do the opposite and search excluding a specific term? Something like:

?&term=NOTabc

Related posts

Leave a Reply

2 comments

  1. The basic explanation

    1. You have a template tag that is called is_search() to determin if you’re on a search page or not.
    2. This then calls get_search_template() which basically is a wrapper function for get_query_template('search').
    3. When you look into the last function, then you’ll see that it basically does locate_template(), which checks for file existence and then does a load_template().
    4. There your $wp_query->query_vars get checked and the $_template_file gets extracted and loaded.

    A note before about the query var …

    WordPress uses the query var s to save the searched term into its query. If you’re running a custom query string, then you need to modify the get_search_query filter like the following:

    /**
     * Modify search query var
     * Doesn't need to be wrapped into esc_attr(), as it's already done by core
     * 
     * @param string $s | The query var to save the search in
     * @return string $s
     */
    function change_search_query_var( $s )
    {
        $s = 'your_custom_query_var';
    
        return $s;
    }
    add_filter( 'get_search_query', 'change_search_query_var', 20, 1 );
    

    Some solution(s)

    We now have different possible solutions:

    The easy solution

    Runs in the template and better fits for excluding taxonomy terms.

    A) Get all posts in the template loop, check every post in the loop for every (taxonomy) term 1) and then exclude/don’t show it.

    In you loop in your template search.php file:

    if ( have_posts() ) {
        while ( have_posts() ) {
            the_post();
    
            global $post;
            // Skip if the post has the term attached
            if ( is_object_in_taxonomy( 
                $post->ID,
                'YOUR_TAXONOMY',
                array( 'TERM A', 'TERM B', '...' ) 
            )
            continue;
        } 
    }
    

    This is just an example of the loop.

    The sophisticated solution(s)

    Runs before running the actual main query and suits better for excluding terms in general.

    B.1) Filter the term out of of the search template query.

    In the following example I use the posts_clauses filter to show you that you can modify even more with just one filter (do a var_dump of the $pieces array for more insights). You could also use the posts_where filter, which runs before the clauses filter.

    // In your functions.php file
    /**
     * Modify the search query where clause
     * Like escapes the term for security reasons
     * 
     * @param array $pieces | The array of post clauses: Where, Group by, etc.
     * @return array $pieces
     */
    add_filter( 'posts_clauses', 'alter_search_query', 10, 2 );
    function alter_search_query( $pieces, $query )
    {
        // Target all search queries in the front-end:
        if( is_admin() || ! $query->is_search() ) return $pieces;
    
        global $wpdb;
        $term = $wpdb->esc_like( 'YOUR_TERM' );
    
        $pieces['where'] .= $wpdb->prepare( 
            " AND   {$wpdb->posts}.post_title NOT LIKE '%s'",
            "%{$term}%" 
        );
    
        return $pieces;
    }
    

    B.2) Filter the term out of of the search template query _in a more performant & specific way.

    In the following example I use the posts_search filter to show you how you can modify the where clause only for the search query. It’s pretty much the same as the posts_where filter.

    // In your functions.php file
    /**
     * Modify the search query where clause
     * Like escapes the term for security reasons
     * 
     * @param array $pieces | The array of post clauses: Where, Group by, etc.
     * @return array $pieces
     */
    add_filter( 'posts_search', 'alter_search_where', 10, 2 );
    function alter_search_where( $search_term, $query )
    {
        // Target all search queries in the front-end:
        if( is_admin() || ! $query->is_search() ) return $search_term;
    
        global $wpdb;
        $term = $wpdb->esc_like( 'YOUR_TERM' );
    
        $search_term .= $wpdb->prepare( 
            " AND   {$wpdb->posts}.post_title NOT LIKE '%s'",
            "%{$term}%" 
        );
    
        return $search_term;
    }
    

    Footnotes:

    1) You haven’t been clear if it’s about a search or a taxonomy term.

  2. Very detailed and thorough answer @kaiser has posted above. I wanted to use first method, however looks like that the first method’s function used in Current wp 4.7 does not work.

    We have to use other function WordPress has that is called: has_term instead of is_object_in_taxonomy to check for specific terms in taxonomy assigned to current post.

    Here is final code which worked for me by using has_term function in your search.php any custom page template you are using for search results, in my case it skips property listings from the search if the listing status is SOLD or RENTED:

    if ( have_posts() ) : while ( have_posts() ) : the_post();
    
    //Skip the post if property taxonomy rl_status is sold or rented: 
        if(has_term(array( 'sold', 'rented' ), 'rl_status', get_the_ID() ){
    
            continue;
                }