I’ve set up the following relationship filter. In the relationship it’s using the ‘home listing’ custom post type information. Within that home listing ACF field group there is a post object field that allows the admin editor to select a community to associate the home listing with. Easy right? Well, not for me because I’m pulling my hair out trying to figure out how to get the damn community post title (which I’m using Post Object field for this because I only want the editor to select one community and the UI is simple) However, it’s the only bit of info in the following code that WILL NOT RENDER on the backend in the ‘show_as_community_model’ relationship field. As you can see
add_filter('acf/fields/relationship/result/name=show_as_community_model', 'my_acf_fields_relationship_result', 10, 5);
function my_acf_fields_relationship_result( $text, $post, $field, $post_id ) {
$community = get_field ('community', $community->post_title);
$sqft = get_field('sq_ft', $post->ID);
$mls = get_field('mls', $post->ID);
$price = get_field('price',$post->ID);
if ($post->post_type == 'home_listings') {
$text .= ' • Community: ' . $community . ' • Lot: ' . $lot . ' • SqFt: ' . $sqft . ' • MLS: ' . $mls . ' • Price: ' . $price . '';
}
return $text;
}
Here’s the screenshot where you can see on the backend how it’s only outputting the Community: text string I manually typed into the filter.
See my changes
add_filter('acf/fields/relationship/result/name=show_as_community_model', 'my_acf_fields_relationship_result', 10, 5);
function my_acf_fields_relationship_result( $text, $post, $field, $post_id ) {
$community = get_field ('community', $post->ID);
$sqft = get_field('sq_ft', $post->ID);
$mls = get_field('mls', $post->ID);
$price = get_field('price',$post->ID);
if ($post->post_type == 'home_listings') {
$text .= ' • Community: ' . $community->post_title . ' • Lot: ' . $lot . ' • SqFt: ' . $sqft . ' • MLS: ' . $mls . ' • Price: ' . $price . '';
}
return $text;
}
Thank you John, I did end up figuring this out today but I’m grateful that you’ve taken the time to reply with the fix. I was really at my wits end and it was crazy simple after I realized the correct syntax. I can’t believe how it’s always the simple little things that can make you pull your hair out lol.
John, I created a separate Forum thread for this question but wanted to direct your attention to this. I need to enable to search this Post Object that this filter is set up on to allow to search more than just the post title but rather those custom fields such as sq ft, community, mls and price. This way the agents have an easier time finding the listing their looking for in the backend of WP.
Here’s the link to the forum post: https://support.advancedcustomfields.com/forums/topic/search-custom-fields-not-just-post-title-in-relationship-post-object-field/
You must be logged in to reply to this topic.
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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.