Skip to content

Instantly share code, notes, and snippets.

@espiat
Forked from aderaaij/acf-future-posts.php
Created March 10, 2022 10:59
Show Gist options
  • Save espiat/b9b11536512be582f8935a95c2f74f4e to your computer and use it in GitHub Desktop.
Save espiat/b9b11536512be582f8935a95c2f74f4e to your computer and use it in GitHub Desktop.
Advanced Custom Fields datepicker: Show only events with a date of today or in the future. Extra comments from Pasnon @ ACF Forums: f you needed to, you could also play with the comparison date, which is the first array in meta_query, currently set to date("Y-m-d") which is today; if you were to make it date("Y-m-d", strtotime("-1 day")) you cou…
<?php
/*
* Display posts only from today and in the future:
* http://old.support.advancedcustomfields.com/discussion/6000/how-to-sort-posts-by-acf-date-and-display-only-future-posts
* by Pasnon
*/
$date_args = array(
'post_type' => 'events',
'meta_key' => 'event_start_date',
'posts_per_page' => -1,
'orderby' => 'meta_value_num',
'order' => 'ASC',
'meta_query'=> array(
array(
'key' => 'event_start_date',
'compare' => '>',
'value' => date("Y-m-d"),
'type' => 'DATE'
)
),
);
$date_query = new WP_Query( $date_args ); ?>
<?php
/*
* Display posts only from today or 30 days into the future: *
*/
$date_args = array(
'post_type' => 'events',
'meta_key' => 'event_start_date',
'posts_per_page' => -1,
'orderby' => 'meta_value_num',
'order' => 'ASC',
'meta_query'=> array(
array(
'key' => 'event_start_date',
'compare' => '>',
'value' => date("Y-m-d"),
'type' => 'DATE'
),
array(
'key' => 'event_start_date',
'compare' => '<=',
'value' => date("Y-m-d", strtotime("+30 days")),
'type' => 'DATE'
)
),
);
$date_query = new WP_Query( $date_args );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment