Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Force WordPress

  • REFUNDED

How can I force WordPress to return only <strong>exact match</strong> search results without using a plugin and without manually putting quote around search terms? By default searching for something like <em>Columbus Ohio</em> will return anything with either <em>Columbus</em> or <em>Ohio</em> unless I put quotes around the search term.

I already tried adding a filter to "get_search_query" that adds quotes around the search term but it didn't change what search results were returned. This was just a wild guess on how to do it but I'm guessing this filter doesn't do what I thought it would and/or I'm using the wrong filter.


add_filter( 'get_search_query', 'my_exactmatchsearch' );
function my_exactmatchsearch( ) {
return '"' . get_query_var( 's' ) . '"';
}


I'm open to other approaches besides this one. Any ideas?

Thanks!



I figured this problem out on my own. It was a matter of using the right hook and changing the query variables. Here's my solution. It's not perfect but it did the trick.

add_filter('query_string', 'my_exactmatchsearch');
function my_exactmatchsearch($query_string) {

// get the query string so we can alter the search term.
$query_string_array = array();
parse_str($query_string, $query_string_array);

if( is_search() ) {
// wrap the search term in quotation marks
$search_value = '"' . $query_string_array['s'] . '"';

// put our value back in the querystring
$query_string_array['s'] = $search_value;
}
return http_build_query($query_string_array, '', '&');
}

Answers (5)

2011-10-11

Kannan C answers:

did you tried this?
http://stackoverflow.com/questions/5991069/search-filter-exact-match


Kevin McGillivray comments:

Hi. Thanks for your reply. I looked at that solution but I'd rather find a way to do it within WordPress rather than using jQuery.

2011-10-11

Julio Potier answers:

I think it depends on your search form from theme.
Can i have a lok into theme files ? ftp/admin account provided ? is it possible ?

Thank you in advance


Kevin McGillivray comments:

No, Julio, that's not possible and we're talking about WP's default search functionality not anything theme-specific. Thanks!

2011-10-11

Abdessamad Idrissi answers:

Mybe it is better to view it from another perstpective: the search function is made to <strong>maximize chances</strong> in finding things. In your case, you want to take off all the power the search function has and weaken it.. It doesn't sound logical.

The best approach is to let search do what it was built for, and use tags (or categories/taxonomies) to find precise keywords.


Kevin McGillivray comments:

"Maximize chance" is a matter of perspective. My client believes that the exact match approach is appropriate for their visitors and I tend to agree in this case.


Abdessamad Idrissi comments:

global $query_string;

You can try this on your search.php
$query_args = explode("&", $query_string);
$search_query = array();

foreach($query_args as $key => $string) {
$query_split = explode("=", $string);
$search_query[$query_split[0]] = urldecode($query_split[1]);
} // foreach

$search_query['s'] ="'".$search_query['s']."'";

//var_dump($search_query);

$search = new WP_Query($search_query);


Have a nice day :)


Abdessamad Idrissi comments:


global $query_string;

$query_args = explode("&", $query_string);
$search_query = array();

foreach($query_args as $key => $string) {
$query_split = explode("=", $string);
$search_query[$query_split[0]] = urldecode($query_split[1]);
} // foreach

$search_query['s'] ="'".$search_query['s']."'";

//var_dump($search_query);

$search = new WP_Query($search_query);


Kevin McGillivray comments:

Thanks for the code snippet. I dropped this code into search.php right before it goes into the loop and nothing happened. The search results that were returned were exactly the same as before this code was installed. Any other ideas?


Abdessamad Idrissi comments:

You can use the [[LINK href="http://codex.wordpress.org/Plugin_API/Filter_Reference/posts_where"]]posts_where[[/LINK]]filter.


Abdessamad Idrissi comments:

try with this to see what the actual query is:

function filter_where( $where = '' ) {
echo $where;
}

add_filter( 'posts_where', 'filter_where' );
$query = new WP_Query( $query_string );
remove_filter( 'posts_where', 'filter_where' );

2011-10-11

Linda answers:

Hi, you might try this. It is a nice link for an Advanced Contextual Search.

[[LINK href="http://weblogtoolscollection.com/archives/2004/06/07/advanced-contextual-search-for-wordpress/"]]http://weblogtoolscollection.com/archives/2004/06/07/advanced-contextual-search-for-wordpress/[[/LINK]]


2011-10-11

Luis Cordova answers:

when I search for "for example" you get this converted to:


?s="for+example"&submit=Search


so get_query_var is returning "for+example" already if i add quotes manually

if I miss the quotes I get:


s=for+example&submit=Search


So you want to remove the + and rather parse it introducing the space %20

add_filter( 'get_search_query', 'my_exactmatchsearch' );

function my_exactmatchsearch( ) {
$originalSearch = get_query_var( 's' );
$filteredOutput = str_replace("+", " ", $originalSearch); // try " " or "%20"
return $filteredOutput; // try this or '"'.$filteredOutput.'"';

}


then you should see on your url:
?s="for%20example"&submit=Search // or
?s=for%20example&submit=Search
which is what you want.

Thanks!