Problem
I have a foreach loop grabbing posts inside an advanced custom fields repeater. When it's finished looping through posts it breaks the repeater. I believe the problem is with $post as far as my hours of debugging have let on. If I remove the foreach loop the times loop through properly. I'm using an ACF Object field to get the posts.
Should do:
date
time
event
event
event
time
event
event
event
date
time
event
event
event
time
event
event
event
Doing:
date
time
event
event
event
date
time
event
event
event
So with the foreach loop it's breaking the next time slot. So all the dates only show one of the time slots. If I remove the loop for the events then all the time slots appear. I've tried putting the foreach loop in different places and it breaks other loops so it's not confined to this.
Structure
I've created an event schedule using two custom post types "Events" and "Schedule". The schedule has posts called "Date". Using this and Advanced Custom Fields I have made a list of dates/times for events.
$args = array (
'post_type' => 'schedule',
'pagination' => false,
);
$query_schedule = new WP_Query( $args );
if ( $query_schedule->have_posts() ) { ?>
<div class="schedule-date" id="<?php // slug ?>">
<ul class="dates-list">
<?php while ( $query_schedule->have_posts() ) {
$query_schedule->the_post(); ?>
<?php
$date = DateTime::createFromFormat('Ymd', get_field('schedule_date'));
?>
<div class="date">
<h1><?php echo $date->format('D jS F' ); /* e.g. Mon 4th February */ ?></h1>
<?php
// check if the repeater field has rows of data
if( have_rows('schedule_times') ): ?>
<ol class="times">
<?php // loop through the rows of data
while ( have_rows('schedule_times') ) : the_row(); ?>
<li class="time-slot">
<h2 class="time"><?php the_sub_field('schedule_time'); ?></h2>
<?php
// Loop Help http://www.advancedcustomfields.com/resources/post-object/
$event_posts = get_sub_field('schedule_events');
if( $event_posts ): ?>
<ul class="list-events">
<?php
echo '<pre>';
print_r( get_sub_field('schedule_events', false, false ) );
echo '</pre>';
?>
<?php
foreach( $event_posts as $post): ?>
<?php setup_postdata($post); ?>
<li class="event">
<h3 class="event-title"><?php the_title(); ?></h3>
<p class="event-location"><?php the_field( 'event_location' ); ?></p>
</li>
<?php endforeach; ?>
</ul>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
</li>
<?php endwhile; ?>
</ol><!-- .times -->
<?php else :
// no rows found
endif;
?>
</div><!-- .date -->
<?php } ?>
</ul>
</div><!-- schedule-date -->
<?php } else {
// no posts found
}
// Restore original Post Data
wp_reset_postdata(); ?>
setup_postdata
in yourforeach
loopwp_reset_postdata
after the loop?Display data for multiple post objects
, the one that doesn't usesetup_postdata
.