Problem
I am looping through custom post types (Advanced Custom Fields) in WordPress. I only want to display events with start_date equal to $newdate variable, defined in the beginning.
The start_date is in the format YYYY-MM-DD HH:mm (the same as $newdate). $newdate is set to the beginning of the day so I wouldn’t exclude events with different hours in the day and compare is set to greater than (just to test the query).
However I am not getting any results.
<?php
$newdate = date('Y-m-d 00:00');
//<-- Start the Loop. -->!
$args = array(
'post_type' => 'epsa_events',
'posts_per_page' => 5,
'orderby' => 'meta_value',
'order' => 'ASC',
'meta_query' => array (
array(
'key' => 'start_time',
'value' => $newdate,
'compare' => '>=',
'type' => 'datetime'
)
)
);
$loop = new WP_Query( $args );
Try this query:-
I guess you have some small mistakes here.
First, if you use
'orderby' => 'meta_value'
you must add a'meta_key' => KEYVALUE
to your$args
whereKEYVALUE
is the name of the custom field you want to use for sorting. CheckWP_Query
documentation on Order & Orderby Parameters.Second, assuming your
start_date
field type is Date Time Picker, if you want to get all events that already started you’re using the wrong comparison operator. So assuming you also want to sort by date, your$args
code should be:Take a look at Date Time Picker documentation too.
I hope this helps!