0

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(); ?>
7
  • try removing setup_postdata in your foreach loop
    – Milo
    Commented Dec 9, 2014 at 3:37
  • I tried removing it and the problem persists. I've done a lot of different configurations. My base attempts started here: advancedcustomfields.com/resources/post-object
    – Jake
    Commented Dec 9, 2014 at 3:43
  • did you also remove wp_reset_postdata after the loop?
    – Milo
    Commented Dec 9, 2014 at 3:45
  • yes, doesn't appear to affect any of the output.
    – Jake
    Commented Dec 9, 2014 at 3:46
  • you said if you remove the foreach loop it fixes it, so effectively stopping anything the foreach loop does should also fix it, something doesn't make sense here. also, the method I'm suggesting is what is shown in the second example under Display data for multiple post objects, the one that doesn't use setup_postdata.
    – Milo
    Commented Dec 9, 2014 at 3:51

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.