Skip to content

Instantly share code, notes, and snippets.

@jchristopher
Created August 31, 2020 12:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jchristopher/2aa11e089b2e6253651348bc2c7aa631 to your computer and use it in GitHub Desktop.
Save jchristopher/2aa11e089b2e6253651348bc2c7aa631 to your computer and use it in GitHub Desktop.
Force quoted search logic in SearchWP when applicable
<?php
// Force multiple word searches to use quoted search logic if quotes are not added.
// NOTE: Quoted search must be enabled (checkbox on the Advanced tab)
add_filter(
'searchwp\query\search_string',
function( $search_string, $query ) {
// If there are already quotes, bail out.
if ( false !== strpos( $search_string, '"' ) ) {
return $search_string;
}
// If there's only one word, bail out.
if ( false === strpos( $search_string, ' ') ) {
return $search_string;
}
return '"' . $search_string . '"';
},
30, 2
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment