Support

Account

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/