ACF relationship field search include custom fields

I am using the code below to modify the acf/fields/relationship/result in order to include extra field data to the list. However, this data is not searchable if I use the relationship field’s search feature.
Can anyone come up with a way I can extend the search function to include this data? I understand I have to use acf/fields/relationship/query to do this, but I do not know how.

Here is the code that adds the relevant data to the list:

Read More
function id_relationship_result( $title, $post, $field, $post_id ) {
  // load a custom field from this $object and show it in the $result
  $city = get_field('city', $post->ID);
  $state = get_field('state', $post->ID);

  // append to title
  $title_new = $state . ', ' . $city . ' ' . $title;

  // return
  return $title_new;
}

Any help is appreciated….

Related posts

1 comment

  1. You’re not too far off here – you just need to use a different ACF hook. To modify the title on return, you have to use the ACF acf/fields/relationship/result hook – which is explained here – http://www.advancedcustomfields.com/resources/acf-fields-relationship-result/

    So, in your case here, you could add the following to your functions.php file

    add_filter('acf/fields/relationship/result/name=your_relationship_field_name', 'id_relationship_result', 10, 4);
    function id_relationship_result($title, $post, $field, $post_id){
        // load a custom field from this $object and show it in the $result
        $city = get_field('city', $post->ID);
        $state = get_field('state', $post->ID);
    
        // append to title
        $title = $state . ', ' . $city . ' ' . $title;
    
    
        // return
        return $title;
    }
    

    That will hopefully allow your title to list the data you need and allow for easier filtering

Comments are closed.