1
votes

I currently have a query for an "Event" category that looks for the ACF event_date field.

Can I use the ACF Repeater option to add multiple event dates to a single post, and then update the query so that it will repeat the same post with multiple event dates in the loop multiple times in order based on the event dates associated with post?

Will that work, repeat a post in the loop using the ACF repeater for multiple event dates?

Current Query with single event_date field.

<?php 
    $args = array(
        'post_type' => 'post',
        'tax_query' => array(
            array(
                'taxonomy' => 'category',
                'field' => 'slug',
                'terms' => 'events'
            )
        ),
        'meta_query' => array(
            array(
                'key'     => 'event_date',
                'value'   => date("Y-m-d"),
                'compare' => '>',
            ),
        ),
        'posts_per_page' => 5,
        'meta_key' => 'event_date',
        'orderby' => 'meta_value',
        'meta_type' => 'NUMERIC',
        'order' => 'ASC'
    );
    $query = new WP_Query( $args );
    ?>

    <?php if ( $query->have_posts() ) : ?>

    <!-- the loop -->
    <?php while ( $query->have_posts() ) : $query->the_post(); ?>

        <?php 
            $date = null;
            if( get_field("event_date") ){
                $date = date_format( date_create( get_field("event_date") ), "F j, Y" );
            }
        ?>
        <div class="program-event-wrapper">
        <?php if( $date): ?>
            <h5><?php echo $date; ?></h5>
        <?php endif; ?>
            <p>
                <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
            </p>
        </div>
    <?php endwhile; ?>
    <!-- end of the loop -->

    <?php //wp_reset_postdata(); ?>


    <?php endif; ?>
1

1 Answers

-1
votes

I think I have gauged what you are after.

<?php if( have_rows('your_repeater_field_name') ): ?>

    <p><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></p>

    <?php while ( have_rows('your_repeater_field_name') ) : the_row(); ?>

        <?php 
            $date = null;
            if( get_sub_field("event_date") ){
                $date = date_format( date_create( get_sub_field("event_date") ), "F j, Y" );
            }
        ?>

        <div class="program-event-wrapper">
            <?php if( $date): ?>
                <h5><?php echo $date; ?></h5>
            <?php endif; ?>
        </div>

    <?php endwhile; ?>

<?php endif; ?>

I have created the ACF Repeater for your dates where if at least one date exists, the title and link are shown. Then, the ACF repeater will go through each row and get the sub field (named event_date) and attached it to your variable and you would run your code you have provided to echo the date, all within the ACF while loop.

FYI, you would replace your_repeater_field_name with your repeater field name.

I hope this makes sense but let me know if you need anything expanding (if I have gauged what you are after correctly).