Support

Account

Home Forums General Issues Show only future event

Solved

Show only future event

  • Hello.

    I’m trying to make event ticker and I want to show only events that are happening on the current day or in the future. But I don’t know how to hide past events. Could anyone help me, please?

    I have a test template with a this code

    <?php $event = new WP_Query(array(
    	'post_type'			=> 'gacr-event',
    	'posts_per_page'	=> 5,
    	'orderby'			=> 'meta_value_num',
    	'order'				=> 'DESC'
    )); ?>
    
    <?php $date = DateTime::createFromFormat('Ymd', get_field('date'));
    	 ?>
    
    <?php while($event->have_posts()) : $event->the_post(); ?>  
        
            <li>
                
                Posted on: <?php the_field('date'); ?><a href="<?php the_permalink() ?>"><?php the_title(); ?></a>
    
            </li>
               
    <?php endwhile; ?>       
  • you could do it after query with php with code like that:

    <?php while($event->have_posts()) : $event->the_post(); 
    $today = date('Ymd');
    $event_date = get_field('date');
    if (strtotime($today) <= strtotime($event_date)) {
    ?>
    <li>
     Posted on: <?php the_field('date'); ?><a href="<?php the_permalink() ?>"><?php the_title(); ?></a>
    </li>
    <?php } endwhile; ?>

    else you need to work with meta_value that is always build like 20150430 year month day without spaces, no matter what input or output format you give the date field with acf settings for that field.

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

The topic ‘Show only future event’ is closed to new replies.