Problem:
The problem with your current snippet is that you are just checking the global main query object, no matter what the current query object is.
Workaround:
Note that the second input argument for the posts_where
filter callback, is the current query object.
Use that to determine if it’s the main search query on the front-end with:
add_filter( 'posts_where', function ( $where, \WP_Query $q )
{
if( ! is_admin() && $q->is_main_query() && $q->is_search()) // No global $wp_query here
{
// ... your modifications
}
return $where;
}, 10, 2 ); // Note the priority 10 and number of input arguments is 2
There’s also the posts_search
filter for the WHERE search part, if that’s what you need to modify.
But in general I would say only modify the generated SQL by hand if, you really must and have no other alternatives.
Related Posts:
- Get Posts updated or published within the last x hours
- Run posts_where and posts_join only on the main query
- How to trim white space in search terms?
- Custom SQL query slows down when using multiple OR … LIKE … in posts_where filter
- Right filter for rewriting page statuscode
- Remove the post_content search from WHERE clause (and CONCAT sql function)
- using posts_where for meta data on pre_get_posts
- How do you disable the verification process of user email changes?
- Not able to change wp_title using add_filter
- How to pass/get data to/from the WooCommerce data-product_variations object?
- Add quick edit fields without custom colum
- Prevent 404 when using pre_get_posts to filter an archive page
- Hook added to the_content seems to be called multiple times
- Translating an error message
- Display WooCommerce newest product reviews on top [closed]
- Overriding wp_get_archives() apply_filters()
- How to make post and comment count unclickable with dashboard_glance_items hook
- How can I hide all posts that don’t have a thumbnail?
- Add Dropdown menu using “add_filter => wp_nav_menu_items”
- Restrict filter to run only inside specific function
- How to add a custom filter in functions.php
- 4.0 remove_filter for WordPress core function not working for me
- How do I add a current class to the current post?
- How apply_filters work in WordPress?
- Apply a filter only once
- What functions are included in apply_filter(‘the_content’)
- How do we check if the user is logging in or registering?
- Add a header before fields added with the attachment_fields_to_edit() filter
- Are there Limitations on filter handles?
- Hide content editor for posts after approriate date
- Buddypress: Edit activity when new blog post [closed]
- How to make a button?
- Same Conditionals Not Working on Two Different Hooks
- remove_action in plugin file
- How to apply filter at search of woocommerce products?
- Filter get_template_part() $args array
- Modify a Filters Second Parameter
- Variables in post title
- How to edit dashboard search posts button texts for my CPT?
- Insert term when page is published – avoid duplicates after edits
- Filter Media by Featured on Admin
- When does a function assigned to the content_filtered_edit_pre filter hook fire?
- Capture post content before page renders
- Empty string supplied as input when parsing content
- How can I remove the kses filters when saving a specific post type ? (it breaks my JSON)
- Remove_action does not work
- How to access plugin variables from theme templates without using globals?
- How to pass variables to custom filter from multiple functions
- WooCommerce Custom Product Validation [closed]
- Make WP not format code, not insert line breaks in between tags
- add img class to native wordpress galleries
- the_content filter together with require_once returns 1 instead of the content of the included file
- Missing Argument 2 for apply_filter
- How to get a single hook from wp_head()?
- WP filter to alter admin CSS styles?
- preview_post_link for Custom Post Types
- How to apply a filter to an ACF wysiwig editor field output?
- Why this filter hook is not working when passing parameters?
- Is it possible to bind a function to a filter hook via Ajax?
- Change the template when the user is not logged in using page_template filter (it does not work)
- Regarding post text filtering
- Add Filter not working with get_posts
- How to Prevent WordPress to encode html in post?
- Filter Pages by Custom Field (ACF) in admin area
- Filter posts by meta data using custom query
- Remove tags without a specific meta key from “choose from the most used tags”
- Which Filter Do I Use To Modify The Subject Of The Retrieve Password Notification Email?
- Register users by e-mail
- automatic title through filter
- Filter causing loss of _wp_attachment_metadata
- Action hook to control access to certain parts of my site
- Buddypress Filter Multiple Activities [closed]
- How can I tranlslate post date in italian?
- Use alternative front page if cookie is set
- Having wp-admin on different domain
- Use a functions in functions.php to remove a string for template theme
- how to output HTML tags in post_content and not as plain text
- oembed_dataparse filter not running at all for standard YouTube embed
- Action for opening attachment or manipulating all attachment links
- Filter wp_mail based on content type
- Add filter conditionally to a page
- mu-plugins body_class filter not working
- Post filter Month dropdown at front-end like wordpress backend
- shortcode function outputs multiple anchor tags
- WP Dashboard -> Posts-> Filter by Category -> Form Method Change : Which Hook
- Show child theme for users on specific IP
- Remove action added from class
- wp.getPosts with status = ‘trash’ using node.js
- Is there a way to overwrite a filter used in canonical.php?
- Adding an orderby filter, casting postmeta with multiple keys
- Identify admin/logout and more filters
- confusion with add_filter
- post->post_content filter
- How to create a list of terms who’s posts also have a predefined external term?
- How to change this WP logo and posts url in block editor?
- How to elect position of new item output in a dropdown when using add_filter
- Query Multiple Filters, one with Meta
- How to remove a filter that is an object method?
- Filter a custom post type by custom field in admin
- Search and Filter Using Custom Post Type, Custom Taxonomy, and Advanced Custom Fields