Skip to content

Instantly share code, notes, and snippets.

@jchristopher
Created May 31, 2014 20:22
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 jchristopher/cdb6130c994b9feec8cd to your computer and use it in GitHub Desktop.
Save jchristopher/cdb6130c994b9feec8cd to your computer and use it in GitHub Desktop.
Replicate term whitelist exclusivity when searching in SearchWP (normally it's just when indexing)
<?php
function my_searchwp_search_regex_whitelist_exclusivity( $terms, $engine ) {
global $searchwp;
// we want to strip out any partial matches
$terms = ' ' . $terms . ' ';
$whitelisted_terms = $searchwp->extract_terms_using_pattern_whitelist( $terms );
$terms = str_replace( ' ', ' ', $terms );
// remove the matches
if( ! empty( $whitelisted_terms ) ) {
$terms = str_ireplace( $whitelisted_terms, ' ', $terms );
}
// clean up the double space flag we used
$terms = str_replace( ' ', ' ', $terms );
$terms = trim( $terms );
return $terms;
}
add_filter( 'searchwp_terms', 'my_searchwp_search_regex_whitelist_exclusivity', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment