Skip to content

Instantly share code, notes, and snippets.

@phlbnks
Last active January 31, 2018 15:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phlbnks/5dd45f319479d2640bbbb35de3793efc to your computer and use it in GitHub Desktop.
Save phlbnks/5dd45f319479d2640bbbb35de3793efc to your computer and use it in GitHub Desktop.
Exclude posts with a term in a taxonomy from search
<?php
/**
* Exclude posts with term 'bananas' from Search.
*/
function cc_search_filter( $query ) {
if ( ! is_admin() && $query->is_main_query() ) {
if ( $query->is_search ) {
$query->set(
'tax_query',
array(
array(
'taxonomy' => 'fruit',
'field' => 'slug',
'terms' => 'bananas',
'operator' => 'NOT IN'
),
)
);
}
}
}
add_action( 'pre_get_posts', 'cc_search_filter' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment