ACF get_posts only outputting once in a loop of posts when using conditional

I’m developing a custom WP theme and using Advanced Custom Fields and Custom Post Type UI and experiencing a problem when trying to:

Use a query to retrieve some custom posts a user has made(the site is for a band and the custom post types are their announcements for
Check if a custom field(“get directions” and “event page”) is filled in and to post it if so.

The relevant code is as follows:



// get posts
$posts = get_posts(array(
‘post_type’ => ‘show’,
‘posts_per_page’ => 4,
‘meta_key’ => ‘show_date’,
‘order’ => ‘DESC’

if( $posts ): ?>

<?php foreach( $posts as $post ):

setup_postdata( $post )


<div class=”row show”>
<div class=”col-sm-6″>
<?php if ( has_post_thumbnail() ) {//check for post image
the_post_thumbnail( array(200,200) );
<div class=”col-sm-4″>
<h3><?php the_title(); ?></h3>
<p><?php the_content(); ?></p>
<span id=”show-date-time”><?php $date = DateTime::createFromFormat(‘dmY’, get_field(‘show_date’)); echo $date->format(‘D jS F Y’); ?>, <?php the_field(‘show_time’); ?></br></span>
<span class=”show-links”>

<?php if( get_field(‘get_directions’) ): ?>
<a href=”<?php the_field(‘get_directions’); ?>”>Get Directions</a>
<?php endif; ?>

<?php if( get_field(‘event_page’) ): ?>
<a href=”<?php the_field(‘event_page’); ?>”>Event Page</a>
<?php endif; ?>


<?php endforeach; ?>

<?php wp_reset_postdata(); ?>

<?php endif; ?>

This loop outputs each show post they’ve entered, other data from each post(the_title(), the_content(), etc.), and even other custom fields just fine(including show_date and show_time, as entered by the user). However, when it comes to “get directions” and “event page”, these are only appearing on the first outputted post on their site.

It would seem that the presence of a conditional is causing this behaviour, for when I remove the if statements around each of the troublesome variables, they appear on each outputted post. The conditionals are implemented as per this user guide entry from ACF. I also used this guide from ACF in implementing the loop originally.

Is the issue with the get_posts function? Should I be using WP_Query instead? Any and all advice is hugely appreciated.

Read more here:: ACF get_posts only outputting once in a loop of posts when using conditional

Leave a Reply

Your email address will not be published. Required fields are marked *