Support

Account

Home Forums Backend Issues (wp-admin) Query Date Picker by Date then Time

Solved

Query Date Picker by Date then Time

  • I’ve made an events section using the date picker and then a time field, amongst other fields.

    I am using this following query to get all future dates only from today’s date then displaying them in ascending by date.

    $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 ),
    );

    What I need to do now is further break this query down so that any events on the same date will query ascending by date and then by start time.

    Does anyone know how I can do this?

  • –Edit–

    Most of this didn’t quite do what we needed – rather than adding to the confusion, I’m just removing this. The solution is in my post below.

  • In an interesting twist of fate, I’m doing this exact thing for my client as well – so I’ll update this and confirm if the above works, or if not, what I did instead.

    Update

    Ignore my previous post, I found a more practical way of doing this here: https://support.advancedcustomfields.com/forums/topic/how-to-orderby-multiple-meta_key/

    WP core have revamped the orderby function to accept array inputs, so after a bit of poking around, this is the method that I’ve used and confirmed:

    $todays_date = current_time('Ymd');
    $args = array(
        'post_type' => 'events',
        'posts_per_page' => '-1',
        'meta_query' => array(
            'relation' => 'AND',
            'date_clause' => array(
                'key' => 'event_date',
                'compare' => '>=',
                'value' => $todays_date,
            ),
            'time_clause' => array(
                'key' => 'event_time',
                'compare' => 'EXISTS',
            ),
        ),
        'orderby' => array(
            'date_clause' => 'ASC',
            'time_clause' => 'ASC',
        )
    );
  • Time Picker and Date/Time picker have been added in Version 5.3.9, which should make this easier

  • But how far out is that? I’m on the most recent version and it’s only 4.4.7. I need a solution now, not 6 months from now.

  • Edd – This query shows only upcoming events while not displaying past events and then sorts by date and if on the same date then by start time?

  • Sorry, I didn’t realize that you are using ACF4. I tend to think that people are using 5 unless they say differently.

  • I wasn’t aware they had multiple wordpress plugins.

  • ACF4 is the version that is available on the WP repo. ACF5 is currently only available in as a premium plugin https://www.advancedcustomfields.com/pro/.

  • Edd – This worked! Thank you! Now I just have to sort out how to order am from pm without using military time or if using military time how to display 1600 as 4pm.

  • ACF Pro has it’s own forum.

  • Most people do not go to the Pro forum to ask questions specifically about Pro. Some do, but there is no rule that says they must. Again, sorry for interrupting this conversation with useless info.

  • Gotcha! By the way, ACF 4 (free) has a Date/Time Picker plugin add-on that I was able to install and sort out the time.

  • Thanks for voting that as the solution!

    With regards to your date/time issue, I personally used the 24 hour clock as it was easier, especially for sorting. To output it differently though, you can grab the time and then reformat it using the DateTime::createFromFormat function (http://php.net/manual/en/datetime.createfromformat.php)

    // format Hi is 24 hour format hours and minutes, both with leading zero
    // i.e. 1630, 0625, 1942 etc.
    $event_time = DateTime::createFromFormat('Hi', get_field('event_time') );
    
    //Output in 12 hour format without leading zeros and with am/pm.
    // i.e. 12.30pm,  9.12am,  6.05pm
    echo $event_time->format('g.ia');
    
  • This helped me resolve my needs too. Thank you so much.

  • Edd – in your application have you looked into having the events fall off by time of the current date?

  • Hi @svsdnb,

    No – I hadn’t done, but you could effectively just add that to the code to replace the meta query that checks that time EXISTS with something more akin to the date check.

    
    $todays_date = current_time('Ymd');
    $right_now = current_time('Hi');
    $args = array(
        'post_type' => 'events',
        'posts_per_page' => '-1',
        'meta_query' => array(
            'relation' => 'AND',
            'date_clause' => array(
                'key' => 'event_date',
                'compare' => '>=',
                'value' => $todays_date,
            ),
            'time_clause' => array(
                'key' => 'event_time',
                'compare' => '>=',
                'value' => $right_now,
            ),
        ),
        'orderby' => array(
            'date_clause' => 'ASC',
            'time_clause' => 'ASC',
        )
    );
    

    I have yet to look at the Time picker field as part of the latest version, but something akin to that should work fine.

  • Hi Edd – when I tried this query out it displayed zero events. I even changed $right_now to h:mm TT as its set in the Time Picker. Or is my problem that I’m using the time picker?

    My time picker field is set to event_start_time

Viewing 18 posts - 1 through 18 (of 18 total)

The topic ‘Query Date Picker by Date then Time’ is closed to new replies.