WordPress Custom Post Disapear After Certain Date

I am developing a website for a client at the moment (currently hosted on my own domain – http://jamiemcardle.com) and we have added a section on the homepage that shows upcoming events. The website is a custom WordPress Theme and there are two post types on the website: the regular WP post and a custom post type called ‘events’. I need the events post type to automatically disappear/delete once their date has passed.

<?php $args = array( 'post_type' => 'event', 'posts_per_page' => 9 );$loop = new WP_Query( $args ); while ( $loop->have_posts() ) : $loop->the_post(); ?>
    <div class="text-center">
        <div class="feature">
            <i class="icon inline-block mb30 fade-0-4 fa fa-calendar-o" style="font-size: 38px !important;"></i>
            <h4 class="uppercase bold"><?php the_field('date'); ?></h4>
            <h5><?php the_field('description'); ?></h5>
        </div>
    </div>
<?php endwhile; ?>

So whenever the value of the date field has passed it should be exempt from the above code. I hope I have explained myself clearly and looking forward to learning from any possible solutions. Thanks for any help in advance,

Read More

Jamie.

Related posts

1 comment

  1. It looks like you’re using Advanced Custom Fields with your “Events” custom post type. Assuming that’s true and you’re using the Date Picker ACF type you could declare two variables, one for the Event’s date, the other for today’s date then compare them. If the Event’s date is the same date as today or in the future then add them to the DOM, otherwise don’t add it.

    <?php $args = array( 'post_type' => 'event', 'posts_per_page' => 9 );$loop = new WP_Query( $args ); while ( $loop->have_posts() ) : $loop->the_post(); ?>
        <?php $event_date = get_field('date'); // Get the event's date ?>
        <?php $today = date('F j, Y'); // Get today's date ?>
        <?php if ($event_date >= $today) : ?>
            <div class="text-center">
                <div class="feature">
                    <i class="icon inline-block mb30 fade-0-4 fa fa-calendar-o" style="font-size: 38px !important;"></i>
                    <h4 class="uppercase bold"><?php the_field('date'); ?></h4>
                    <h5><?php the_field('description'); ?></h5>
                </div>
            </div>
        <?php endif; ?>
    <?php endwhile; ?>
    

Comments are closed.