Skip to content

Instantly share code, notes, and snippets.

@imagebox
Last active May 21, 2022 07:11
Show Gist options
  • Save imagebox/c61b3c10d570ef5216928ebceadb6a44 to your computer and use it in GitHub Desktop.
Save imagebox/c61b3c10d570ef5216928ebceadb6a44 to your computer and use it in GitHub Desktop.
[WordPress | Query Posts, Show Upcoming Events Posts By ACF Date] #WordPress #ACF
// not for the Events Calendar posts but for an Events CPT or sommething similar that uses ACF fields for dates.
// example from https://idelic.com/resource-type/events/
<?php
$today = current_time('Ymd');
$post_query_args = array(
'post_type' => 'resources',
'posts_per_page' => -1,
'status' => 'publish',
'tax_query' => array(
array (
'taxonomy' => 'resource_type',
'field' => 'slug',
'terms' => 'events',
)
),
'meta_query' => array(
array(
'key' => 'event_start_date',
'compare' => '>=', // Upcoming Events - Greater than or equal to today
'value' => $today,
)
),
'meta_key' => 'event_start_date',
'orderby' => 'meta_value',
'order' => 'ASC',
);
$post_query = new WP_Query( $post_query_args );
?>
<?php if ( $post_query->have_posts() ) : ?>
<?php while ( $post_query->have_posts() ) : $post_query->the_post(); ?>
<!-- your bullshit -->
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php endif;
?>
@oneblackcrayon
Copy link

Line 40 is why I starred this gist.
Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment