Hi,
I can’t get the acf/fields/relationship/result
hook to work no matter what I try.
Here is the code I’m using:
function profile_relationship_results( $title, $post, $field, $post_id ) {
$first_name = get_field('profile_first_name', $post->ID);
$last_name = get_field('profile_last_name', $post->ID);
$title = $first_name.' '.$last_name;
return $title;
}
add_filter('acf/fields/relationship/result/name=about_team_profiles', 'profile_relationship_results', 10, 4);
I have tried different variable names, messing with $post_id
, returning a hard-coded string but no matter what I do the relationship field always shows null. The number of custom post displayed is correct, that is how I know that the relationship is querying the right post type.
I thought the problem might be that the post type I’m looking up doesn’t have a title, but I tried with another post type which has it and it doesn’t work either.
I’m probably missing something rather simple. Any help is much appreciated.
Thanks!
Since it’s showing nothing it does seem to be working. It appears from your description that the fields have no values. I would double check the field names. Also I would look at if these fields are sub fields of another field.
The fields are not subfields and I double-checked the field names as well. Even if I return a hard-coded dummy value it doesn’t show up and I only get null.
So it seems like the filter is activated but somehow the return $title
is not getting through at all.
Any ideas on how to debug this?
My only guess at this point is that there is another filter for the same thing somewhere that is running after this one.
I found a solution. The issue was with my configuration based on the combination of Trellis/Bedrock/Sage by Roots.
I was putting the above-mentioned code into the setup.php
, but it should go into the filters.php
(duh…). Also, I had to inline the function as such:
add_filter('acf/fields/relationship/result/name=about_team_profiles', function($title, $post, $field, $post_id) {
$first_name = get_field('profile_first_name', $post->ID);
$last_name = get_field('profile_last_name', $post->ID);
$title = $first_name.' '.$last_name;
return $title;
}, 10, 4);