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 >
<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
CodePudding user response:
So the issue is due to $post
being a global variable. I assume ACF stores an array of posts for get_field('select_post_author')
then you need to change your foreach
statement local variable name, e.g:
<?php foreach( array_slice($postAuthors, 0, 1) as $postAuthor): ?>
<div >
<span>
<?php echo get_the_date('M d, Y', $postAuthor->ID); ?>
</span>
</div><!-- /.post-date -->
<?php endforeach; ?>
CodePudding user response:
I solved the issue by passing the post id in the date.
<?php
$pid = get_the_ID(); //get the post id
$postAuthors = get_field('select_post_author');
if( $postAuthors ): ?>
Pass the post id in the get_the_date.
echo get_the_date('M d, Y',$pid);