The pre_get_posts filter is the right place to go. You can check if you are on a search query, and alter it the way you want. Here’s an example:
function search_filter($query) {
if ( !is_admin() && $query->is_main_query() ) {
// Check if we are on a search page
if ($query->is_search) {
// Get the search query
$search_term = get_search_query();
// You can modify the search query here, and pass it to the query again.
$search_term = str_replace( "-", "" , $search_term );
$query->set( 's', $search_term );
}
}
}
add_action('pre_get_posts','search_filter');
Related Posts:
- How to reset usual $query on search page to push custom $wpdb query there?
- Run search query again without pagination gives no results?
- what are the numbers between curly brackets in search query
- Search custom post type by meta data
- Custom query filter not working on woocommerce category page
- Search Terms – Querying on either description__like OR name__like in the same Term Query?
- $query->query_var[‘post_type’] not set for pages
- Filter query posts by the count of metas for a meta key
- search through post-type attachments titles
- How to set back query var author_name after unset it?
- pre_get_posts : ‘post__not_in’ doesn’t work with global variable
- Search query -> Show all pages except certain template
- Temporarily storing main search result
- Replace/Mute/Stop Search Query
- Query based on title, with ‘compare’ => ‘IN’
- is_search called incorreclty
- Attempt to improve WP search, can someone check my SQL query?
- Searching With Apostrophe
- How to change the amount of posts previewed on a page?
- Ideas how to search & replace post_content when string contains a newline?
- How to insert Collate into WordPress search query?
- Slow Search Queries – How to exclude pages, media, excerpt, authors, private posts?
- How to interfere to default search to make it search in custom fields?
- How to manage a particular “order by” for get_search_query()?
- Merge CPT Taxonomy and Post Taxonomy in $query->set
- WordPress search in modified post title
- Filter orders by modify date
- How to add url parameter to every search query in SearchWp?
- Add rewrite rule for archive search
- custom sorting media with multiple filters – error: not unique table/alias: wp_postmeta
- Restrict WordPress search to a single ACF field
- Modify behaviour of “s” parameter in wp_query
- Sort by postmeta on when searching
- Pagination adds search query (again)
- Speed up search query that searches in post meta?
- New custom post type entries are not sorted correctly in admin using pre_get_posts
- How to make MySQL search queries with quotes
- Limiting a filtered query
- $query->set( ‘post_type’, ‘post’ ); not working
- Why the pre_get_posts hook can cause apache stopped working?
- search.php is not generating the right results
- excludeCat function reverses order of blog posts
- Set posts per page for parent category and it’s all children
- Filter multiple different main queries on custom pages
- How to display search query as formatted text?
- Modify a query using both a text field and a true/false AFC field
- Admin Post List table Query filtering “WHERE” for custom post type
- Search query alteration not working for meta values
- pre get posts not working with Tribe Events
- is_search called incorrectly
- How to allow searching a custom meta key in admin list table?
- Multiple Category Query
- How to exclude pages from WP search based on page title
- list all categories that have a certain word in title
- Passing parameters to a static front page
- wp_list_pages() refuses to output posts
- Can’t seem to do combined query AND sort?
- why pre_get_posts works fine in post type archive, but not in search result list?
- sort query results by newest
- Order by meta value pro first then meta value free in my search function
- Get a list of posts by specific category
- How to query two meta fields and display results between them
- How to delete all posts, categories and tags from WordPress database
- Sorting search results with custom dropdown
- Query the WordPress database to get data together with replaced information
- $wpdb->prepare affecting the query?
- WordPress SQL search, how to handle SQL Injection?
- Query for Custom Post Type UI Does Not Loop All the Post
- Updating with $qpdb->query() always returns 0 rows affected
- Query to view scheduled or draft post
- How to add a Variable to post query
- Exclude taxonomy Query if post_type
- Sort a list of a CPT ( job) if a meta is empty
- Block internal search queries with pre_get_posts and regex rules
- Build A Custom SQL Query for WordPress Search
- Trying to get all links in my posts
- Search Exact on WordPress – Relevannsi Solution Not Working
- Creating a custom WP query for a Custom Post Type
- How to filter result set in custom metaboxes?
- Restore WordPress Backup Locally?
- Trouble migrating custom post types from non-wordpress cms
- “order” does not affect order of custom query
- Create a new post on a specified publish date via link?
- Exclude Posts from a Widget
- Query String for the WP_QUERY parameters
- $wpdb query for price in custom field value
- Multiple terms not working on taxonomy
- Sorting of coupons in the right way
- Get query result before posts are displayed?
- Custom post types loop on a page template
- WP_Query orderby meta key/value suddely stopped working
- Form Data Causes “Page Not Found”
- Where can I find the SQL to get the most used information by wordpress database?
- WooCommerce Total # orders [closed]
- How do I find users by password?
- Sorting a custom post type in pre_get_posts
- Why does $_GET return values but get_query_var does not?
- Sort users in admin screen by meta value whether it exists or not
- Include posts under a custom taxonomy in author.php
- Custom API how to return int field