In the post loop I have a foreach loop which displays selected team members in the admin(from the "team" custom post type) from an ACF post object.
What I want to achieve is to display the post-publish date inside the foreach.
The problem is that the code echo get_the_date('M d, Y');
shows the date of the "team" custom post type and not the blog post date because is in the foreach.
I tried to pass the post id but it’s shows the posts id of the "team" post types and not the actual "post".
How can I show the 'post_type' => 'post'
publish post date inside in the foreach?
<?php
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 10,
'orderby' => 'date',
'order' => 'DSC'
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
$postAuthors = get_field('select_post_author'); // ACF post object - custom post type - team
if( $postAuthors ): ?>
<?php foreach( array_slice($postAuthors, 0, 1) as $post): ?>
<div class="post-date">
<span>
<?php
echo get_the_date('M d, Y'); // here I want to get the post publish date but it show the publish date of the team post type items
?>
</span>
</div><!-- /.post-date -->
<?php endforeach; ?>
<?php endif; ?>
<?php endwhile;
wp_reset_postdata();
?>
Thanks
2
Answers
I solved the issue by passing the post id in the date.
Pass the post id in the get_the_date.
So the issue is due to
$post
being a global variable. I assume ACF stores an array of posts forget_field('select_post_author')
then you need to change yourforeach
statement local variable name, e.g: