0

I’m trying to make event ticker with Advanced Custom Fields 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?

This is my code. It works fine, but it doesn't hide past events.

<?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; ?>
2

You need a meta query. There's also documentation on ACF for querying dates.

<?php

$event = new WP_Query(
    array(
        'post_type'       => 'gacr-event',
        'posts_per_page'  => 5,
        'orderby'         => 'meta_value_num',
        'order'           => 'DESC',
        'meta_query'      => array(
            array(
                'key' => 'date',
                'type' => 'NUMERIC', // MySQL needs to treat date meta values as numbers
                'value' => current_time( 'Ymd' ), // Today in ACF datetime format
                'compare' => '>=', // Greater than or equal to value
            ),
        ),
    )
);
  • Aaaa, such a simple thing and I couldn't figure out how. Thank you so much, you are my hero. It works now! – Kailo Apr 29 '15 at 13:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.