Home › Forums › Backend Issues (wp-admin) › Modify the relationship field search › Reply To: Modify the relationship field search
you can hook into the query used by ACF using this hook https://www.advancedcustomfields.com/resources/acf-fields-relationship-query/
Altering what the search is searching on the other hand is a different story.
Let’s say that you wanted to alter every search performed by an acf relationship field, please not, that the following is only a basic outline of where you might start based on what I know.
// add a filter that will run when a relationship query is being done
add_filter('acf/fields/relationship/query', 'add_acf_relationship_pre_get_posts_filter', 10, 3);
function add_acf_relationship_pre_get_posts_filter($args, $field, $post_id) {
// this filter does not really do anything
// it runs just before acf does the query
// so that we can hook a pre_get_posts filter
// only when acf is doing a query for a relationship field
add_filter('pre_get_posts', 'acf_relationship_pre_get_posts_filter');
return $args;
}
function acf_relationship_pre_get_posts_filter($query) {
// remove this filter so it will not run again
remove_filter('pre_get_posts', 'acf_relationship_pre_get_posts_filter');
if ($query->is_search()) {
// modify the query?
}
}
Also, you’re going to need to alter the where statement of the sql query and you may need to add another filter for this that also gets removed when it it run, you should see this https://presscustomizr.com/snippet/three-techniques-to-alter-the-query-in-wordpress/ and maybe this http://adambalee.com/search-wordpress-by-custom-fields-without-a-plugin/
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.