Support

Account

Home Forums Add-ons Repeater Field How do I only show future events?

Solving

How do I only show future events?

  • Hello.

    I want to show only events that are happening on the current day or in the future.
    I have an “Events” page with a repeater field (I haven’t used a custom post type for this). Users enter the event date via the “date_picker” field.

    It seems there’s a few answers using WP_Query, but that only works with a custom type, right?

  • Apologies, I am a bit of a newbie to this. I should have included my code.

            
    <?php if(have_rows('event')): ?>
                <?php while(have_rows('event')): the_row() ?>
                    <article>
                        <h2><?php the_sub_field('event_title') ?></h2>
                        <section class="metadata">
                            <ul class="clearfix">
                                <li><?php the_sub_field('event_date') ?></li>
                                <li><?php the_sub_field('event_start_time') ?> - <?php the_sub_field('event_finish_time') ?></li>
                                <li><?php the_sub_field('event_venue') ?></li>
                            </ul>
                        </section>
                        <?php the_sub_field('event_description') ?>
                    </article>
                <?php endwhile; ?>
            <?php endif; ?>
    
  • Hi eike,

    You need to set up args that filter the date. I actually have this set and running on my site.

    I set the timezone to my timezone and then set 2 variables, one is set to todays date and the other is set to a year from now.

    <?php 
    	//Set server timezone to central
    	date_default_timezone_set('America/Chicago'); 
    	//Today's date
    	$date_1 = date('Ymd', strtotime("now")); 
    	//Future date - the arg will look between today's date and this future date to see if the post fall within the 2 dates.
    	$date_2 = date('Ymd', strtotime("+12 months"));
    ?>

    Then I set a variable to determine if the post is in the future:

    //arg to determine if the post is an upcoming event.
    	$upcoming_args = array(
    		'post_type'		=> 'event',
    		'posts_per_page'	=> -1,
    		'meta_key' => 'start_date',
    		'meta_compare' => 'BETWEEN',
    		'meta_type' => 'numeric',
    		'meta_value' => array($date_1, $date_2),
    		'orderby' => 'meta_value_num',
    		'order' => 'ASC'
    	); 
    ?>

    Set up a new upcoming event query:

    <?php 
    	// the upcoming events query
    	$upcoming_query = new WP_Query( $upcoming_args ); 
    ?>

    Then the loop:

    <?php if ( $upcoming_query->have_posts() ) : ?>					
      <!-- the loop -->
      <?php while ( $upcoming_query->have_posts() ) : $upcoming_query->the_post(); ?>
      
      	<!--Your Content-->
      								    
      <?php endwhile; ?>
      <!-- end of the loop -->
    
      <?php wp_reset_postdata(); ?>
    
    <?php else:  ?>
      <p><?php _e( 'There are currently no upcoming events, please check back soon.' ); ?></p>
    <?php endif; ?>

    Hope that helps.

  • Hey guys!

    I have the same issue, i want to display future post only.
    I tried the solution Jason typed out.

    Here is my code.

    `<?php

    //Set server timezone to central
    date_default_timezone_set(‘Europe/Amsterdam’);
    //Today’s date
    $date_1 = date(‘Ymd’, strtotime(“now”));
    //Future date – the arg will look between today’s date and this future date to see if the post fall within the 2 dates.
    $date_2 = date(‘Ymd’, strtotime(“+12 months”));

    ?>

    <?php
    //arg to determine if the post is an upcoming event.
    $upcoming_args = array(
    ‘post_type’ => ‘programma’,
    ‘posts_per_page’ => -1,
    ‘meta_key’ => ‘start_date’,
    ‘meta_compare’ => ‘BETWEEN’,
    ‘meta_type’ => ‘numeric’,
    ‘meta_value’ => array($date_1, $date_2),
    ‘orderby’ => ‘meta_value_num’,
    ‘order’ => ‘ASC’
    );
    ?>

    <?php
    // the upcoming events query
    $upcoming_query = new WP_Query( $upcoming_args );
    ?>

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

    <?php the_field(‘datum’); ?>

    <?php
    if( have_rows(‘programma_tijden_2’) ): ?>
    <div id=”programma_tijden_2″>

    <?php

    // loop through rows (parent repeater)
    while( have_rows(‘programma_tijden_2’) ): the_row(); ?>
    <div class=”even-testen”>

    <!– wrap the_sub_field in elk een aparte div voor de styling –>

    <?php the_sub_field(‘tijdstip’); ?> <br />
    <?php the_sub_field(‘informatie’); ?> <br />
    <?php the_sub_field(‘extra_informatie’); ?> <br />

    </div>
    </div>

    <?php endwhile; ?>
    <?php endif;?>

    <?php endwhile; ?>

    <!– end of the loop –>

    <?php wp_reset_postdata(); ?>

    <?php else: ?>
    <p><?php _e( ‘Er zijn momenteel geen evenementen meer.’ ); ?></p>
    <?php endif; ?>
    `

    The only thing i see on my site is there are no events (the else statement)
    What am i missing?

    i’am not creating a new post, because i’am using this code. if needed i will create a new post.

    thnx in advance

  • @arjen_m Are you using a field named “start_date” in ACF? For my code, the argument looks in that field to see if the date falls between today’s date and 12 months from now. If it is not called “start_date” you will need to change the following to whatever your field is called.

    ‘meta_key’ => ‘start_date’,

  • Awesome! thank you! that was it.

    i have one more question,
    on the website it shows only 1 future post. when that date is reached and past, it displays the next post.

    is it possible to get all the future posts?

    i tried to change the ‘post_per_page’ => ‘-1’ to something else, but that doesn’t do the trick.

  • never mind! it is working now! when i saved the post again. it displays on the website

    thanks!!

Viewing 7 posts - 1 through 7 (of 7 total)

The topic ‘How do I only show future events?’ is closed to new replies.