Skip to content

Instantly share code, notes, and snippets.

@mishterk
Last active September 18, 2022 23:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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 );
@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