ACF: Showing Upcoming Events

In this example, my ACF field is named ‘event_date’ and is attached to a custom post type called ‘events’.


<?php

$upcoming_args = array(
	'post_type'      => 'events',
	'meta_key'       => 'event_date',
	'posts_per_page' => 6,
	'orderby'        => 'meta_value_num',
	'order'          => 'ASC',
	'meta_query' => array(
			array(
				'key'     => 'event_date',
				'compare' => '>=',
				'value'   => current_time( 'Ymd' ),
			),
	),
);

$upcoming_events = new WP_Query( $upcoming_args ); ?>

<?php if ( $upcoming_events->have_posts() ) : ?>

	<?php while ( $upcoming_events->have_posts() ) : $upcoming_events->the_post(); ?>

		<?php $date = get_field( 'event_date' ); ?>
		<?php the_title(); ?>: <?php echo esc_html( $date ); ?>

	<?php endwhile; ?>

<?php wp_reset_postdata(); ?>

This will display the next 6 upcoming events by date in comparison to the user’s current date.

Additional Reading:

Leave a Reply

Your email address will not be published.