Home › Forums › Backend Issues (wp-admin) › Modify the relationship field search
I have a question and idea regarding the relationship field for ACF.
Issue:
The search isn’t great. If you have a relationship field and you type “Foo Posts” but the post title is “Foo Post’s” with an apostrophe you don’t get a match. There’s no support for fuzzy matching or even support for ignoring special characters.
I assume this is just using the native WordPress search since the results are the same.
Solution:
Is it possible to tap into how the search is performed? If it’s possible to use a search library in between what the user inputs and using that with a fuzzy search library like Fuse.js we could get more accurate results.
I know this wouldn’t be a part of ACF core but it may work as an extension.
If someone is familiar with how this could work and could point me in the right direction I can take it from there.
Thanks!
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/
Thanks @hube2!
I’m going to mark this as solved. My specific question requires some custom dev work and this at least helps me get started.
If I do end up making this as a plugin or I have a function that’s working I’ll be sure to drop this into the thread for anyone who’s interested.
And one day.. maybe one day… we can bring real fuzzy search to ACF.
🤞
Cheers! — Dan
The topic ‘Modify the relationship field search’ is closed to new replies.
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.