Skip to content

Instantly share code, notes, and snippets.

@mishterk
Last active September 18, 2022 23:16
Show Gist options
  • Save mishterk/b8a3946bdde62fe4de2b211d0271debc to your computer and use it in GitHub Desktop.
Save mishterk/b8a3946bdde62fe4de2b211d0271debc to your computer and use it in GitHub Desktop.
How to show the age of each post in an ACF relationship field. For more info see https://philkurth.com.au/tips/customise-the-post-titles-in-acf-relationship-field/
<?php
add_filter( 'acf/fields/relationship/result/name=related_posts', function ( $title, WP_Post $post, $field_arr ) {
$posted_at = get_post_time( 'U', false, $post->ID );
$now = current_time( 'timestamp' );
$diff = human_time_diff( $posted_at, $now );
return $title . sprintf( ' (%s ago)', $diff );
}, 10, 3 );
@whynotadv
Copy link

How would I add a simple text based ACF custom field in the relationship filter you have setup? I can't seem to get it right. I have a field named lot where the user can just type in an ACF text field. Right now the Post Object only shows the Title of the WP Post coming from the CPT of home_listings. Which I'm using a relationship field named: show_as_community_model

This way sales agents at this home builder can choose a related home listings as their model home at their prospect communities and be able to see the lot number. I'd also like to show the mls number too. The field is named 'mls'

Thank you!

@mishterk
Copy link
Author

Have you tried using ACF's get_field() function?
https://www.advancedcustomfields.com/resources/get_field/

@whynotadv
Copy link

I worked out a solution using get_field thank you. The tough part was that a field in the field group was set as a “post object” so I couldn’t use $post->ID for that field type but rather “post-title”

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment