0
votes

I have a Repeater Group setup using Advanced Custom Fields Pro in Wordpress. I want to set up a script that will remove/hide the repeater row in the front-end after the date picker date is after the current date.

Here is the code snippet i'm using for the page to display the date rows:

<?php 

if (have_rows('location')) {
        ?>
    <div class="tab-pane fade" id="location_name" role="tabpanel"
            aria-labelledby="location-tab">
        <?php while (have_rows('location')) {
            the_row(); ?>
                <div class="tab_col usual_text date_col">
                    <?php
                    $orgDate = get_sub_field('date');
                    $newDate = date("l, F d", strtotime($orgDate));
                    echo $newDate; ?>
                </div>
                <div class="tab_col usual_text time_col">
                    <?php the_sub_field('time'); ?>
                </div>
                <div class="tab_col additional_info">
                    <?php the_sub_field('additional_info'); ?>
                </div>
                <div class="tab_col btn_col">
                    <a class="dark_btn" href="<?php the_sub_field('buy_url'); ?>">
                    </a>
                </div>
            </div>
        <?php } ?>
    </div>
<?php } ?>

My goal is to look at the sub field "time" and compare it with the current date and if it has past, not display this whole row.

Even further, if all of these rows have past, I want to identify that and return a different layout. I just need to identify what if/else statement I could write to determine all these rows are gone.

Any assistance would be great! Thank you.

1
It sounds like you're looking to do something with JavaScript to detect the current date. Do you have a code sample so I can provide the best answer possible for your question?Connor Smyth
Thanks! I updated the original question to include code plus some additional details. Let me know if that helps!VertigoSFX
So I think I misunderstood your question initially but the code snippet helped provide context for me. Are you looking to compare the date and time fields with the current date and time and if the current date and time, then if the current date and time is past the field date and time then do not render the fields on the front end. Is this correct?Connor Smyth

1 Answers

0
votes

Here is the snippet based on my understanding of the question now. Thanks for providing the snippet and more context, it helped in my ability to answer the question.

The specific code addition is to create a DateTime object from the date and time fields. Then to create a DateTime object which will default to the current date and time. Then compare the two and if the current date-time is greater than the date-time from the fields then you don't render them on the front end.

The fields DateTime object is wrapped in a try-catch statement in case there is any formatting error in the returned strings. If there is an error it will default to not showing them on the front end. This means that you must ensure that the date-time fields are able to be passed to the DateTime constructor. To see how to format the fields correctly you can see this section in the PHP docs https://www.php.net/manual/en/datetime.formats.php

<?php if (have_rows('location')) : ?>
    <div class="tab-pane fade" id="location_name" role="tabpanel"
            aria-labelledby="location-tab">
        <?php while (have_rows('location')) : the_row(); ?>
            <?php
            $field_date = get_sub_field('date');
            $field_time = get_sub_field('time');

            $field_date_time = false;
            try {
                $field_date_time = new DateTime($field_date . $field_time);
            } catch (Exception $e) {
                error_log($e);
            }

            $current_date = new DateTime();

            if ($current_date != false) :
                if ($current_date > $field_date_time) : ?>
                <div class="tab_col usual_text date_col">
                    <?php
                    $orgDate = get_sub_field('date');
                    $newDate = date("l, F d", strtotime($orgDate));
                    echo $newDate; ?>
                </div>
                <div class="tab_col usual_text time_col">
                    <?php the_sub_field('time'); ?>
                </div>
                <?php endif;
            endif; ?>

            <div class="tab_col additional_info">
                <?php the_sub_field('additional_info'); ?>
            </div>
            <div class="tab_col btn_col">
                <a class="dark_btn" href="<?php the_sub_field('buy_url'); ?>">
                </a>
            </div>

        <?php endwhile; ?>
    </div>
<?php endif; ?>