John,
Again thank you. Although I had issues when I tried your function it led me to a solution that worked. I included my code below. Your code resulted in blank fields. It seems, although i am not sure why, that I had to use a parameter of “$object” and call “$object->ID” for each custom field. ~ Thanks again 😉
add_filter('acf/fields/relationship/result/key=field_565de210b1c8c', 'my_relationship_result', 10, 4);
function my_relationship_result( $result, $object, $field, $post ) {
// load a custom field from this $object and show it in the $result
$lname = get_field('last_name', $object->ID);
$fname = get_field('first_name', $object->ID);
$result = $fname.' '.$lname;
return $result;
}
Thanks John,
I’ll poke my head into some querying and/or some pre load options. But to wrap this thread up would you know of a way to hide the default “post_title”?
I appreciate all of your guidance on this. I learned something new along the way.
Regards,
~Jeffrey
So to be clear….
“If you skip trying to order them by the fields that would work.”
I’m sorry but that confuses me somewhat. Are you saying John that my only real option in using the “acf/fields/relationship/result” filter is to return IDs?
In the meantime I did try out the result filter and as you can see it is pulling the “LAST_NAME, FIRST_NAME” FIELDS (see attached pic).
So is it possible for
1) Remove the Post Title so only my query results are showing?
2) Order on the Query
function my_relationship_result( $result, $object, $field, $post )
{
// load a custom field from this $object and show it in the $result
$lname = get_field('last_name', $object->ID);
$fname = get_field('first_name', $object->ID);
$result .= ' [' . $lname . ', ' . $fname . ']' ;
return $result;
}
add_filter('acf/fields/relationship/result/key=field_565de210b1c8c', 'my_relationship_result', 10, 4);
John,
Thank you so much for the insight and guidance. I have gleamed from further research that the filters are iffy in the order actions as you mentioned.
In my case I have a ‘first_name’ and ‘last_name’ CFs. My goal is to inject a Relationship field that lets the user select the Teacher and of course order that list by ‘last_name’. Obviously the Relationship field is pulling and utilizing the title for presentation. I am not even sure if i can change this. Not really my issue here.
As everything stands right now everything works outside of the order. I can see how the suggested solution might be able to work for me but I would love just a little clarification. I am brand new to ACF so i appreciate any further guidance if possible.
Q: But are you saying I should dynamically have the ‘last_name’and/or ‘first_name’ populate a Select Field and then have that be the new relationship field (ie ‘first_and_last_name’)?
Q: If so then does that infer i would set the order of the list BEFORE the Relationship pulls the data in from that dynamic Select field?
Again, I want to thank you John for taking the time to guide me in the issue.
~Jeffrey
Ugh… i did not format Approach #2 code… here that is:
add_filter('acf/fields/relationship/query/key=field_565de210b1c8c', 'exclude_staff', 10, 3);
function exclude_staff( $args, $field, $post )
{
$args = array(
'post_type' => 'dwb_directory',
'meta_key' => 'last_name',
'orderby' => 'meta_value',
'order' => 'ASC',
'suppress_filters' => false,
'paged' => $paged,
'meta_query' => array(
array(
'key' => 'position_type',
'value' => 'teacher',
'compare' => 'LIKE'
)
)
);
return $args;
}
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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.