as the title indicates I am trying to make a query for the post closest to today's date. I know this is a duplicate post but the ones I have found on the topic has not been able to solve my problem.
my code:
$args = array( 'type'=>'post', 'orderby'=>'name',
'order'=>'ASC', 'taxonomy'=>'category' );
$categories = get_categories( $args );
foreach ( $categories as $cat ) {
$args = array( 'post_type' => 'event',
'numberposts' => 1,
'cat' => $cat->cat_ID,
'meta_key' => '_date',
'order' => 'DESC',
'order_by' => 'meta_value',
'no_found_rows' => true,
'meta_query' => array(array(
'key' => '_date',
'value' => date('Ymd'),
'compare'=> '>=',
'type' => 'NUMERIC'
))
);
$query = new WP_Query( $args );
I have been looking to this post for help: Arrange Posts By Date In Order Of Closest To The Current Date
What works: The query does not return post's with a date that has passed.
What does not: The query will not order by the meta key '_date', I always end up with the most/least recently created post rather than what I am trying to achieve.
Any help is appreciated, thank you.