ranchodeluxemedia
2/2/2017 - 8:06 AM

Ordering Events by Advanced Custom Fields Date & Time Picker Field

Ordering Events by Advanced Custom Fields Date & Time Picker Field

<?php
// Credit to http://joshuaiz.com/words/ordering-events-by-advanced-custom-fields-date-time-picker-field/

$time = current_time( 'timestamp' ); // Get current unix timestamp

// Set up custom query with meta_query to compare event start date with today's date
$args = array (
'post_type'              => 'event', // your event post type slug
'post_status'            => 'publish', // only show published events
'orderby'                => 'meta_value', // order by date
'meta_key'               => 'event_start_date', // your ACF Date & Time Picker field
'meta_value'             => $time, // Use the current time from above
'meta_compare'           => '>=', // Compare today's datetime with our event datetime
'order'                  => 'ASC', // Show earlier events first
'posts_per_page'         => 10,
);

$query = new WP_Query( $args );

if ( $query->have_posts() ) :

while ( $query->have_posts() ) : $query->the_post(); // Start loop

// Your custom loop code here

wp_reset_postdata();

endif;
?>