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

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