I’m using the Post Object type field, and in the backend, this gives a select box that shows the titles of the posts and displays the post titles after selection.
I want to add the post publication date to both the selection area and to each post after it has been added. Using this doc https://www.advancedcustomfields.com/resources/acf-fields-post_object-result/ I’ve been able to add the date.
function add_post_object_date( $title, $post, $field, $post_id ) {
$title .= $post_date = get_the_date( 'j F Y' );
return $title;
}
add_filter('acf/fields/post_object/result', 'add_post_object_date', 10, 4);
But, the date displayed is the same for all posts; and the date does not appear when selecting a post to add.
Do I need to reset the query to show the correct publication date for each post? How do I show the post date in the selection area? And, the date appears at the end of the title string; can it be moved to the front?
Thanks
y9u need to specify the post ID https://codex.wordpress.org/Function_Reference/get_the_date
$title .= $post_date = get_the_date( 'j F Y', $post_id );
Thanks, actually, I figured it out; I had to use $post->ID, as $post_id gave me the same date for all posts.
And I used a different concatenation method to put the date first:
function add_post_object_date( $title, $post, $field, $post_id ) {
$post_date = get_the_date( 'j F Y - ',$post->ID);
$date_title = "$post_date $title";
return $date_title;
}
add_filter('acf/fields/post_object/result', 'add_post_object_date', 10, 4);
And FYI for anyone else reading, this sorts the post objects by date in the backend:
add_filter( 'acf/fields/post_object/query', 'change_posts_order' );
function change_posts_order( $args ) {
$args['orderby'] = 'date';
$args['order'] = 'DESC';
return $args;