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?
- WP Query where title begins with a specific letter
- Order Search Results Page by meta_value If no Value Return Remaining Results
- Query WooCommerce orders where meta data does not exist
- Tracing the life of a query
- Get WooCommerce best selling products for the current month [closed]
- Woocommerce query by price range and custom meta key
- Custom search form with empty parameters
- Find all product that contain terms with %keywords% in WP_Query
- WP Query – Get WooCommerce Products with variation that is in stock
- Multiple search queries on one page
- Search product by brand name in wp_query
- WP Query search for attachments and their exact title
- Including only current user’s posts in search
- Custom category search box for WordPress
- Refine search results using WP_Query
- Search Posts with Custom Fields as query
- Extending woocommerce admin product search
- Creating a custom search for a specific post type
- Assigning a meta_query value based on regexp to wp_query fails
- WC displaying products from category
- Is there any advantage of using default WordPress Search instead of creating a custom one?
- Get all products which have both product category
- Search for multiple tags?
- wp_query ‘s’, search filter with pagination is not working
- Sort by price woocommerce by ID post_meta
- Cluster WooCommerce products in each Category Archive by Tags assigned to products
- WP_QUERY to display woocommerce products whose author IS the logged in user
- Display filtered results into custom page
- WooCommerce – get product sub categories
- Cutom wp_query for geolocation search, pagination not working
- Search Query for multiple categories using ‘OR’ but having certain categories be ‘AND’
- 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?
- How do I get my custom query to work with search results after the first page?
- Modify Search Query if original Query gave no results
- Need Sub-category with main Category
- Woocommerce pagination and result count after custom loop
- Include post content of linked posts in search
- Replacing search results with custom external query
- Prevent author bio page from showing in search results
- How do I amend wp_query before it is run/executed?
- WordPress Custom Search Form Displaying Unexpected Results
- Can’t fetch Custom Post Type Data through Custom Query
- How to properly use AND / OR in custom search
- How do I get Woocommerce product variation name and variation description in a WP_Query?
- How to avoid bypass of search bar rules?
- How do you determine if a result in a search query is a post or a page?
- Slow Query On Search
- How To Keep Search Title the same on paged Results
- wp_query show all values
- WP_Query for products always returns empty
- How to restrict search on a certain page to only return results against custom taxonomies?
- Filter out a meta key in the Search results page with two CPT
- Query by key or author
- Ignore image urls in wp_query search
- Can I make a search query which includes a space?
- WordPress search WP_Query to cover multiple post types and their custom fields?
- My combination of ‘post_type’ and ‘tax_query’ not working?
- Woocommerce Get Orders By Meta Value
- Save large WP_Query to transient === false
- Custom Woocommerce Product Query
- WP_Query no result if keyword contains number
- Multiple WP_Query args combinations according to post type
- WooCommerce WP_Query using tax_query returns no results (0 = 1)
- How sort products by calculate value? ( custom post meta, price, option)
- wc_get_products() return empty object
- WooCommerce. How To Exclude Subcategory Products From Category Listing Page
- get_posts return only first result
- How to display Woocommerce products list by tag
- Display the search results like the original pages
- How to use jQuery validation to set the search bar to accept only a specific input?
- Woocommerce featured products query no longer working
- update_post_meta performance in a loop woocommerce
- update_post_meta performance in a loop woocommerce
- getting posts by tags
- Query child posts with tax query on parents
- Custom Post Type WP_Query with filters and search
- WordPress search page pagination displaying but not functional
- Search has query that will return no results
- WP Query – Search in title or author_name
- Get posts by similar names and categories
- How do i query woocommerce booking post type on init action?
- Order & Orderby clause not working with custom query
- How can I modify standard search query to include also ACF custom fields values?
- Woocommerce orderby rand with tax_query not random
- $wp_query when new WP_Query
- Need help setting up a search form for wordpress [duplicate]
- Show linked products using wp query and woocommerce
- WordPress WP_Query Search (‘s’) With Multiple Search Terms
- How does Show Posts based on a keyword search work in WP_Query
- Search results stuck on page 1
- Custom Taxonomy in custom REST API search