Exclude from custom search field
-
I have 2 search areas on my site. One is the site-wide search. The other for searching within certain custom post type, and search the titles only.
Can I activated the ACF search for the site-wide search results only?
-
This topic was modified 6 years, 8 months ago by
sdcr.
-
This topic was modified 6 years, 8 months ago by
-
Hi @solidcolour,
Thank you for your message.
It works for every search. Everywhere you use “s” in query arguments.
If I change the name=”s” on my custom post type search form. The search results are messed up. Is there a way to exclude ACF from the custom search form? But keep it for the global site search form?
Please try this filter:
function acfbs_is_available( $status ) { return true; } add_filter( 'acfbs_is_available', 'acfbs_is_available' );Write inside the condition and return a boolean value.
Source: https://plugins.svn.wordpress.org/acf-better-search/trunk/app/Search/Init.php
Thanks for the quick reply. I tried few ways but couldn’t incorporated it into the search filters below. Help please!
function _s_staff_query($query) { if ($query->is_search() && 'staff' === get_query_var('post_type')) { $query->query_vars['orderby'] = 'name'; $query->query_vars['order'] = 'ASC'; } } add_action('parse_query', '_s_staff_query');Of course, it shouldn’t affect the global site search form, which just uses the default settings, no additional filters for it.
-
This reply was modified 6 years, 8 months ago by
sdcr.
Try use inside function acfbs_is_available:
global $wp_query;If I wrap the search filters into
acfbs_is_availableit worked. However if the plugin is disabled, then everything inside won’t get executed. Is there any other way to turn off ACF search?What other way? You wanted to turn off the search and you can do it using this filter.
How do I turn off the ACF better search for the custom post type search only? In the other words put those together in some way.
function _s_staff_query($query) { if ($query->is_search() && 'staff' === get_query_var('post_type')) { $query->query_vars['orderby'] = 'name'; $query->query_vars['order'] = 'ASC'; } } add_action('parse_query', '_s_staff_query');and
function acfbs_is_available( $status ) { return true; } add_filter( 'acfbs_is_available', 'acfbs_is_available' );I have released update
3.4.1which adds a new filter. He should help you even more and it will certainly be easier to use:/** * Turn off search in ACF Better Search plugin for selected WP_Query * * @param boolean $status The status of whether the search should work. * @param object $query WP_Query Object */ function check_acfbs_search( $status, $query ) { return true; } add_filter( 'acfbs_search_is_available', 'check_acfbs_search', 10, 2 );Thanks for the updates. I copied and pasted it into functions.php It doesn’t seems to do anything for me. If I change it to
return false;then it gets turned off globally. Still want to know if I can turn it off ONLY within:if ($query->is_search() && 'staff' === get_query_var('post_type'))as commented previously. Let me know if the question wasn’t clear. Thanks again for your time.I gave you an example only. You must add your conditions inside and handle it correctly. Example:
/** * Turn off search in ACF Better Search plugin for selected WP_Query * * @param boolean $status The status of whether the search should work. * @param object $query WP_Query Object */ function check_acfbs_search( $status, $query ) { if ( $query->is_search() && ( 'staff' === $query->get('post_type') ) { return false; } return $status; } add_filter( 'acfbs_search_is_available', 'check_acfbs_search', 10, 2 );That’s it! Works beautifully. Thanks a lot.
I am happy to. @solidcolour, could you add a review to the plugin? I will be grateful!
Sure thing. Just did it actually. Thanks again for the great work and support.
-
This reply was modified 6 years, 8 months ago by
The topic ‘Exclude from custom search field’ is closed to new replies.