Support

Account

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

  • 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.