Skip to content

Instantly share code, notes, and snippets.

@searchwpgists
Last active March 9, 2022 13:47
Show Gist options
  • Select an option

  • Save searchwpgists/087d5d3d5b47b2e38c8062f80d7ef04f to your computer and use it in GitHub Desktop.

Select an option

Save searchwpgists/087d5d3d5b47b2e38c8062f80d7ef04f 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