• Resolved svsdnb

    (@svsdnb)


    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.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter svsdnb

    (@svsdnb)

    I was able to convert ACF’s date to YYYY-MM-DD and used this code however it’s returning all “Events”

    $args = array(
            'post_type' => 'events',
    	'post_status' => 'publish',
    	'posts_per_page' => '10',
    	'meta_query' => array(
    				'key' => 'event_date_ends',
    				'compare' => '>=',
    				'value' => date(Y-m-d),
    				'type' => 'DATE'
    			),
    	'orderby' => 'meta_value_num',
    	'order' => 'ASC',
    	'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1 ),
    );
    Thread Starter svsdnb

    (@svsdnb)

    Turns out I just had to convert this via functions.php to unix timestamp and it worked.

    functions.php

    function custom_unixtimesamp ( $post_id ) {
        if ( get_post_type( $post_id ) == 'events' ) {
    	$startdate = get_post_meta($post_id, 'event_date_begins', true);
    
    		if($startdate) {
    			$dateparts = explode('/', $startdate);
    			$newdate1 = strtotime(date('d.m.Y H:i:s', strtotime($dateparts[1].'/'.$dateparts[0].'/'.$dateparts[2])));
    			update_post_meta($post_id, 'unixstartdate', $newdate1  );
    		}
    	}
    }
    add_action( 'save_post', 'custom_unixtimesamp', 100, 2);

    then query

    $today = time();	
    
    	$args = array(
            'post_type' => 'events',
    	'post_status' => 'publish',
    	'posts_per_page' => '10',
    	'meta_query' => array(
    		array(
    			'key' => 'unixstartdate',
    			'compare' => '>=',
    			'value' => $today,
    			)
    			),
    	'meta_key' => 'event_date_begins',
    	'orderby' => 'meta_value',
    	'order' => 'ASC',
    	'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1 ),
    );

    Thread Starter svsdnb

    (@svsdnb)

    I found the actual issue so I thought I’d update.

    ACF documentation says to use $today = date (‘Ymd’) to compare dates but you really need to use current_time(‘Ymd’) so I removed the functions.php code that I added and fixed the problem rather than work around it.

    Here’s my query now

    $event1 = current_time('Ymd');
    	$args = array(
            'post_type' => 'events',
    	'post_status' => 'publish',
    	'posts_per_page' => '10',
    	'meta_query' => array(
    		array(
    			'key' => 'event_date_ends',
    			'compare' => '>=',
    			'value' => $event1,
    			)
    			),
    	'meta_key' => 'event_date_ends',
    	'orderby' => 'meta_value',
    	'order' => 'ASC',
    	'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1 ),
    );

    Thanks so much svsdnb for posting the solution that you found. My case is exact like yours, a custom post type Events.
    Do you have, by any chance, a filter/search of the event list? Like for instance filter by location or order by start_date?

    Thanks again!
    D.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Query Date Array To Display Future Events Only’ is closed to new replies.