If you want exact phrase matching, you need to use quotes. Search for "good friend"
instead of good friend
.
I know that (ordinary users do not know it) and so just in case asked.
Now I am looking for a solution how to create a search form with checkbox “exact match” – where the user search in the address bar will be added to quotes
old
/?s=good+friends
new “exact match” search
/?s=”good+friends”
—————
without plugin – sentence and exact – working, but I like relevanssi and I want to continue to use it
-
This reply was modified 7 years, 3 months ago by kolshix.
Create a pre_get_posts
filter function that adds quotes around the search term when the checkbox is set, it’s simple as that.
That’s right?
in searh form
<input type="checkbox" name="exact-match" value="1" />
</form>
in Functions
function search_exact_match($query) {
if ($query->is_search && isset( $_GET['exact-match'] ) ) {
$exact_match = '"' . get_search_query( s ) . '"';
$query->set( 's', $exact_match) ;
} else {
return false;
}
}
add_action('pre_get_posts', 'search_exact_match');
-
This reply was modified 7 years, 3 months ago by kolshix.
-
This reply was modified 7 years, 3 months ago by kolshix.
Yes, that’s how – though it may be a good idea to check if there are any quotes in the search query before adding your own, multiple quotes may break the search.
Also, get_search_query()
is escaped with esc_attr()
, that may be a problem. Probably better to use $query->query_vars['s']
.
not working with ” ‘ ” – not found
function search_exact_match($query) {
if ($query->is_search && isset( $_GET['exact-match'] ) ) {
$exact_match = '"' . esc_attr( get_search_query( ) ). '"';
$query->query_vars['s'] = $exact_match;
// $query->query_vars['s'] = esc_attr($exact_match);
// $query->query_vars['s'] = '"' . $exact_match . '"';
} else {
return false;
}
}
add_action('pre_get_posts', 'search_exact_match');
if search ” mom’s good friends “- not found
how to set up Search Terms with an apostrophe?
-
This reply was modified 7 years, 2 months ago by kolshix.