Home Forums Backend Issues (wp-admin) Modify the relationship field search


Modify the relationship field search

  • I have a question and idea regarding the relationship field for ACF.

    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.

    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.


  • you can hook into the query used by ACF using this hook

    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 and maybe this

  • 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

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.