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.
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.
You must be logged in to post a comment.
Change it when it gets to WordPress, right before WP queries the database:
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.
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.