If searching via WordPress ?s=keyword (not WooComm’s Product Search), post_type isn’t set or used, so the following would return false:
if( isset($query->query_vars['post_type']) && $query->query_vars['post_type'] == 'product' ) {
You can explore what $query->query_vars contains on your Products page vs your Products Search page by just printing it and killing the page:
function my_pre_get_posts( $query ) {
echo "<pre>".print_r($query->query_vars,true)."</pre>";
die;
//...
If you are using WooComm’s search (and the query is like ?s=keyword&post_type=product) and WooComm does have post_type set, try firing your action with a later priority:
add_action('pre_get_posts', 'my_pre_get_posts',999);
Related Posts:
- Create multiple Search functions for posts / custom post types and everything
- pre_get_posts and search query for admin
- Search function works improperly
- Force is_search to always TRUE
- How to conditionally pass a parameter to wordpress search to limit post types?
- How to exclude pages from WP search based on page title
- Search for two strings in WP search
- Displaying number of search results for each post type
- Use pre_get_posts to display search results by meta in specific order
- Search custom posts by meta key in admin
- pre_get_posts improperly searching revisions
- WP search in metadata post
- meta field search too restrictive in pre_get_post() for custom search
- search filter for search and tag page doesn’t work
- grep for special characters in Unix
- How do I remove Pages from search?
- Remove meta robots tag from wp_head
- WordPress blog with 30 000 posts: poor search performance
- Template issues getting ajax search results
- How do I search events between two set dates inside WP?
- Search Using Post ID
- template_include for search.php makes WordPress think its on the home page
- Include custom table into search results
- How Do I Use WP_Query to Run This Database Query as Search Result?
- Return all Tags from search results
- Search pre_get_posts filter which can handle multiple post types and categories?
- ElasticPress Indexing multiple sites
- How to search through pdf media files?
- WordPress Single Conditional for Search Page
- Do not allow to search certain words
- How to add custom taxonomy to search
- get_query_var(‘s’) not working
- Display title for search engine visitors
- How to avoid the specific page while searching in WordPress without plugin?
- How to show a random selection of posts from the search
- Related Tags Concept
- Default WP search excluding specific characters, is it possible?
- Comparing between a negative and positive number
- Custom search for custom post meta with pre_get_posts interferes with WP search
- Searching by meta values showing inappropriate result
- Search by keywords with boolean operators
- Redirect All Search Links Containing – to + on WordPress
- How to make wordpress Dynamic Search-Bar from Predesigned Static Html Search-Bar?
- Change Search url slug for each site in a multisite network
- URL search link for ‘category__and’ instead of php query
- Search media by title and description – Frontend
- Is there a way to add a custom text + link above search results for different searches?
- How to change tab’s title in search results pages? [closed]
- Custom Taxonomy Not Working When added to WordPress Search
- Customise Excerpt start and end
- Generate list of tags from search results
- Template issues getting ajax search results
- How can I implement Search feature for Membership site?
- Order search results by category
- some one help . search not jump to search page
- Why should we use the name attribute with the value “s” in the creation of the search form?
- The Search FoRM Dilemma || The Search form Templating
- Search for anything in site
- How can I add search box in single.php?
- Connecting Google Site Search with WordPress Taxonomies/Categories/Tags
- Only display post content in search results
- meta post search
- search page not found? (search.php or searchpage.php)
- Media Library / Backend: How can I search for text in the ALT Text field?
- Rewrite wp function
- How to fetch Email addres of an author via ID?
- How to Fix Form elements do not have associated labels in WordPress Search Form (without button)
- How to exclude specific post_type from default search?
- Search issue on special Character
- Modify main search form to search user too
- Removing &submit=Search from search parameters?
- customize search result in wordpress
- Highlight search words in excerpt
- $_GET value is losing it’s space
- Search For Custom Post Type and Custom Fields
- Searching on my blog is not working
- “Modular/reusable” content and search results
- Add Search Form to Dropdown menu
- Search doesn’t work with ‘relation’ => ‘OR’
- Add meta tag to search results
- Searchform for searching specific categories
- Selectbox and Searchform and Custom Search Page
- Hightlight search-terms with functions does remove or disable other filters?
- SQL: Search query to get attachments only of those parents which are published
- Change the text “search result for” on search result page
- Search redirects to index
- Want to creat a search for my wordpress theme [closed]
- Return 404 for /search/ queries – For Genesis
- Deprecating WordPress URL from Google, promoting custom domain [closed]
- Search with multiple selects that include tags
- Search results from a specific database table
- How do I create a search form that searches only within a custom post type?
- Two Querys in one Search – Category-Tag nightmare – HOW?
- Code in the excerpt of the search page
- WordPress REST API response – not returning results from custom taxonomy, only from title
- Members location sortable based on UK Postcode
- I had removed my phone number from my website footer, but months later it still appears once I look it up on google, how to remove it?
- search posts by POST ID
- Combining search and sort in the admin list using pods
- Search only shows results when logged in