Skip to content

Instantly share code, notes, and snippets.

@aderaaij
Last active February 8, 2024 14:29
Show Gist options
  • Save aderaaij/6827478 to your computer and use it in GitHub Desktop.
Save aderaaij/6827478 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 );
@crixlet
Copy link

crixlet commented Nov 19, 2014

This is awesome. Thank you! Had been digging on a solution for this exact issue for a while and this finally worked. I threw this meta_query for the ACF datepicker field into pre_get_posts and it worked perfectly.

@humbertograca
Copy link

Hi There,

Thanks for you tips on how to do that, have another question i would like to be able to set weekly events using the plugin do you have any ideas on how to achieve that? Have been trying but cant find a way on how to do that.
That would be a life saver.

Thanks in advance,

@muzKore
Copy link

muzKore commented Oct 5, 2017

That is slick! Mega props and thanks for the insight.

@avanwart
Copy link

Hello. Just wanted to say this helped me out today. Thank you.

@anartist
Copy link

It is an excellent solution and it helped me so much!

@MathieuLopes
Copy link

👍 Thanks for the gist. Nice solution !

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