Wordpress - Exclude Specific Term from Search
The basic explanation
- You have a template tag that is called
is_search()
to determin if you're on a search page or not. - This then calls
get_search_template()
which basically is a wrapper function forget_query_template('search')
. - 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 aload_template()
. - 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.