I have a Custom Post Type called 'fotos' with a couple ACF fields connected to it.
Inside the while loop in my homepage i'm retrieving the most recent posts from this CPT. I want to show an ACF field called 'clients' for each of these posts. the $photo->ID is correctly returning the ID of the CPT post.
<?php
if (have_posts()) :
while (have_posts()) : the_post(); ?>
<!-- ... -->
<?php
$recent_photos = wp_get_recent_posts(array(
'numberposts' => '8',
'post_type' => 'fotos',
'post_status' => 'publish',
));
if ($recent_photos):
?>
<?php foreach ($recent_photos as $photo): ?>
<div class="item-grid--item">
<?php the_field('client', $photo->ID); ?>
</div>
<?php endforeach; ?>
<?php endif; ?>
<!-- ... -->
<?php endwhile;
else:
_e('Sorry, no posts matched your criteria.', 'textdomain');
endif;
?>
However, the_field, get_field, the_sub_field or get_sub_field return absolutely nothing. What could it possibly be? I checked everything and it's all correct!
My Answer:
It looks like you are not passing the correct post ID to the ACF functions. Instead of using `$photo->ID`, you should use `$photo['ID']` in your code.
Here is the updated code snippet:
php
if (have_posts()) :
while (have_posts()) : the_post(); ?>
$recent_photos = wp_get_recent_posts(array(
'numberposts' => '8',
'post_type' => 'fotos',
'post_status' => 'publish',
));
if ($recent_photos):
?>
else:
_e('Sorry, no posts matched your criteria.', 'textdomain');
endif;
?>
By making this change, you should now be able to retrieve the ACF field values for each post in your Custom Post Type 'fotos'.
Rate this post
5 of 5 based on 3660 votesComments