I’m trying to display the post nearest to todays date but so far, no luck.
Lets say today is 18/10/2014, and i have 2 posts, postA has a date 17/10/2014 and postB has a date 21/10/2014, i want postA to be displayed because is nearest today.
The closest i could get to that result is with this code and i know its still far from what i’m looking for 🙂
$today = date('Ymd');
$date = get_sub_field('fixture-date'); // ACF Date Field
$args = array(
'post_type' => 'events',
'orderby' => 'meta_value',
'meta_key' => $date,
'order' => 'DESC',
'posts_per_page' => 1,
'meta_query' => array(
'key' => $date,
'value' => $today
'compare' => '>='
),
Get the closest post by date
Here is a function that can return you the post by the closest date.
I wrote you some comments to explain you the progress.
You can get only the post->ID or all the post object and do with it what you want.
I did sth like that, it works for me:
it’s important to use functions with ‘get_’. $posttype can be simple ‘post’ or your custom post type.