Hi,
I was wondering what the filter was to change the search term in Wordpress?
For example, if someone types in xxx, how could I change that to yyy before it goes into the wordpress search engine?
Cheers.
Hi,
I was wondering what the filter was to change the search term in Wordpress?
For example, if someone types in xxx, how could I change that to yyy before it goes into the wordpress search engine?
Cheers.
Your not going to be able to change it before it goes into wordpress without using htaccess rewrite rules. What you can do however is create a custom filter to manually redirect specific search terms into new search query's using a standard browser redirect. I had to use the javascript location function in my example because I couldn't figure out how to catch the search variable via a filter before anything was outputted to the browser (thus limiting my ability to use the built in wordpress redirect function or a standard php header redirect.)
The following code will take any searches for "test" and redirect it to a "smickie" search. This was put together pretty quick and dirty, so you'll want to modify it to suite your needs obviously, but hopefully this can get you started in the right direction.
function redirect_searchterm() {
if (is_search()) {
$search_query = get_search_query();
if ($search_query == "test") {
$new_searchquery = "smickie";
?>
<script type="text/javascript">
<!--
location.replace("<?php echo get_option('siteurl') . '/?s=' . $new_searchquery .'&submit=Search'; ?>");
-->
</script>
<?php
}
}
}
add_action('wp_head', 'redirect_searchterm', 1);
Change it when it gets to WordPress, right before WP queries the database:
$search_replacements = array(
'find' => 'replace',
'find2' => 'replace2',
'var' => 'foo'
);
function modify_search_term($request_vars) {
global $search_replacements;
if (!empty($request_vars['s']) && !empty($search_replacements[$request_vars['s']])) {
$request_vars['s'] = $search_replacements[$request_vars['s']];
}
return $request_vars;
}
add_filter('request', 'modify_search_term');
This will allow you to handle as many conditions as you can think up and add to the $replacements array.
The 99 in the add_filter is to get it to run late so that you're the last one to make changes to the query (could be important depending upon what other plugins you have installed).
Your URL will still indicate the original term, but you save a page load. If you have a high traffic site then you don't want to redirect just to get a pretty url.