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
- How to Optimize WP site for millions of posts
- Get Terms by IDs with IDs order
- Slow wp_enqueue_media()
- Advanced Custom Fields – Query Efficiency
- How to display lastest post date in the homepage?
- Can not switch the queried post in pre_get_posts hook
- How to tell if $query_var isset?
- Custom search page and search by title, content and tag
- Sorting Grids with Essential Grid and Events Manger
- The use of including upgrade.php when building custom queries
- Get the timout value of a saved transient?
- Does meta_query work within get_posts array?
- How to show more than 5 posts?
- WordPress Find Duplicate Post By Content
- mySQL query. ORDER BY meta_key
- how to query multiple categories in wordpress?
- Strange string in console from wpdb query
- Insert static element only once in query archive
- How to run a mysql query when admin updates user role?
- WP Query with custom Shortcode
- Missing posts in a query
- Alter a specific query on WordPress
- Protect sequence of pages with same password for each of two groups of users
- How is it possible to get top comment from all children?
- Query to get number of posts last week
- WooCommerce search products between price range using WP_Query
- All in One Calendar Plugin Custom Post Type Query [closed]
- Get Posts from Last 24 hours and Sort them via GD Star Rating
- How to read the value of a WordPress $query associative array (hash) key
- Query posts based on previous query
- Waypoints + infinite scroll with custom query
- Filtering custom posts by custom fields (ACF) [closed]
- How do I get all results from my query
- Taxonomy filter under Polylang
- How to create an overview of posts with the same tag?
- create a link to a random post within the current category
- `offset` WP_Query argument dont work via `pre_get_posts`
- JetEngine Query Builder – order by value from repeater
- Change pure SQL database query to WordPress post query?
- Heavy meta query causing SQL crash
- Pulling an ACF into a query
- How can I modify standard search query to include also ACF custom fields values?
- Using cron for multiple queries
- Need help setting up a search form for wordpress [duplicate]
- very slow wordpress query with default query
- Custom Query problem access the values
- If more than 1 image show post link?
- Rewrite Query_Var URL Parameter with Slug
- How to query different categories on index?
- Can’t See Media Queries with Inspect Tool [closed]
- Search results stuck on page 1
- How to figure out correct wp_options to have autoload=’yes’