pre_get_posts
is fired before the query is run and is used to customize the query before its is run.
See here for a list of hooks, they are listed in order of execution. https://codex.wordpress.org/Plugin_API/Action_Reference
I would suggest using a hook that fires after the query is run for example, template_redirect
is fired immediately after the query is run.
function no_products_found_redirect() {
global $wp_query;
if ( $wp_query->is_search && !have_posts() ){
wp_redirect ( home_url('/request-quote/?product_name=" . get_search_query() ) );
exit;
}
}
add_action( "template_redirect', 'no_products_found_redirect' );
Related Posts:
- showing all search result in one template
- Extend product search with meta in WooCommerce
- Hide products in uncategorized category from search results
- $wp_query->found_posts not returning correct value
- Filtering product search results using tags
- Can I force WP_Query to return no results?
- Post_count only shows the number of results per page
- WP Query where title begins with a specific letter
- WP_Query: Exclude hidden products from WooCommerce product list
- Search custom taxonomy term by name
- How to query only for products with status “in stock” in WooCommerce? [closed]
- Order Search Results Page by meta_value If no Value Return Remaining Results
- Query WooCommerce orders where meta data does not exist
- WP_Query for WooCommerce Products
- Tracing the life of a query
- Want to redirect if search query match exact title of any post
- Changing a theme’s search function to only show results from woocommerce?
- Get WooCommerce best selling products for the current month [closed]
- Woocommerce query by price range and custom meta key
- Pagination not working for Custom search form & custom result template
- How to support multiple search terms query within one process?
- Pagination broken when merging search results with additional WP_Query
- Custom search form with empty parameters
- WooCommerce: filter by parent product’s taxonomy and product variation’s meta data
- How to search by title or tags exclusively?
- WordPress search exact match
- Pagination wont work with search results template [duplicate]
- How do I search WordPress by different fields without a plugin?
- Multiple keyword search
- Retrieve products with specific attribute and in category – woocommerce
- How to combine tax_query and date_query in WordPress
- how to retrieve specific product attribute value in an sql query?
- Using next/previous_posts_link with customised search
- WordPress custom search form with pre_get_posts not work
- Search – Ajax – Alter Query Parameters with Pagination
- Find all product that contain terms with %keywords% in WP_Query
- Add custom search results to main WP search [duplicate]
- Display featured products through custom loop in WooCommerce 3 [closed]
- WP Query – Get WooCommerce Products with variation that is in stock
- Multiple search queries on one page
- tax_query shows no results if nothing is selected
- Search product by brand name in wp_query
- Get product list of given category
- Searching through different categories on different pages code is not working
- How to exclude products by tag from woocommerce shop page?
- WP Query search for attachments and their exact title
- WP_Query’s “request” SQL Query
- Including only current user’s posts in search
- WooCommerce Only OnSale Products Loop Snippet [closed]
- is_search was called incorrectly
- How do I search inside specific taxonomies in WordPress
- Modifying the Search Results
- Custom category search box for WordPress
- Refine search results using WP_Query
- How to find exact match for search term in WP_Query? What is the additional string added in LIKE query in WP_Query?
- How to get the WC_Product Object when using a WP_Query in WooCommerce
- Search Posts with Custom Fields as query
- Extending woocommerce admin product search
- Complex date range with WP_Query and BETWEEN
- Creating a custom search for a specific post type
- merge two query arguments into one WP_Query call
- Custom Search Result Page displaying only 1 result.
- Woocommerce returns Product post_status as published even tho it is in status draft
- How to loop for every result found in the_content() when using the search query?
- Assigning a meta_query value based on regexp to wp_query fails
- WC displaying products from category
- Querying on multiple taxonomies pulled from $_GET checkbox array not working?
- Is there any advantage of using default WordPress Search instead of creating a custom one?
- Get all products which have both product category
- WP_Query Not Recognizing Taxonomy Parameter in Custom Search
- Search pages that are a child of the current page
- Include tags in WP_Query
- Search for multiple tags?
- Woocommerce query all products and categories
- Custom Search Not Consistent with Results
- Take all produts by category
- wp_query ‘s’, search filter with pagination is not working
- Sort by price woocommerce by ID post_meta
- Editing the default wordpress search
- How to get all product attributes for filters in WooCommerce?
- Cluster WooCommerce products in each Category Archive by Tags assigned to products
- How to use wp_query to post count by search term > group post count by date > convert to json. (ex. result 12-21-2020 – 343)
- Multiple Search Terms WP_Query
- WP_QUERY to display woocommerce products whose author IS the logged in user
- WP Query Args – Title or Taxonomy Value
- Display filtered results into custom page
- WooCommerce – get product sub categories
- Having trouble with WooCommerce Storefront child theme single.php “related posts” modification [closed]
- WordPress pagination not working on template search.php
- Cutom wp_query for geolocation search, pagination not working
- wordpress search word, “hello world” ===> ‘hello+world’ ===> ‘hello’, ‘world’
- Search Query for multiple categories using ‘OR’ but having certain categories be ‘AND’
- Woocommerce – Changing the order of the upsell products [closed]
- Out Of memory issue on post per page parameter
- query_vars overriding WP_Query args
- Query with search and subscribers only output
- how can I override The Events Calendar query modifications?
- Search by post id in a specific page then auto-redirect to searched post
- Search for “2nd” also returns “22nd” in wp_query
- How to make search for posts using get method?