Query Date Array To Display Future Events Only
-
I have created a custom post type called “Events” and added in custom fields using Advanced Custom Fields. I have just basic fields since our need for a calendar is basic in itself.
Fields are: Start Date, End Date, Start Time, End Time, Location, Title
I am trying to query posts with an array that is basically “If End Date is greater than todays’s date, Then show posts”.
The issue is Advanced Custom Fields has a built in DatePicker which formats dates as YYYYMMDD and not YYYY-MM-DD as PHP does. Here is my code as it is today:
if (isset($_GET['_m'])) { $current_month = $_GET['_m']; $current_day = date('d'); // the actual day $current_year = $_GET['_y']; $future_year = 2016; $startday = $current_year.$current_month.$current_day; $endday = $future_year.$current_month.$current_day; echo $startday; echo $endday; } $args = array( 'post_type' => 'events', 'post_status' => 'publish', 'posts_per_page' => '10', 'meta_key' => 'event_date_ends', 'meta_query' => array( 'key' => 'event_date_ends', 'compare' => '>=', 'value' => $startday, 'type' => 'DATE' ), 'orderby' => 'meta_value_num', 'order' => 'ASC', 'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1 ), );
I’ve tried at least 20 variations and techniques with the end result being either A) all “Events” that are published are returned or B) No “Events” published are returned.
I’m trying to steer away from using a calendar plugin for various reasons mainly since most are bulky in resources and also the client wants the return values laid out particularly and I haven’t been able to find a calendar plugin that was that customizable. Visually it’s approved as it is now but past events will not automatically be removed on their own.
- The topic ‘Query Date Array To Display Future Events Only’ is closed to new replies.