I’m having some trouble getting an arguments array to sort an event list by the date in WordPress. I’ve found several suggested solutions here on Stack Overflow and elsewhere, but none of the solutions seem to work after lots of trial and error.
It’s nothing fancy, and it should be a lot easier than this. Maybe it is easier and I’m just overthinking it? Any help is greatly appreciated.
I’ve got a custom Date Field inside the Post using the plugin http://www.advancedcustomfields.com, field name in the database is “event_date”.
I’ve tried the following in various forms:
$args = array(
'post_status' => 'publish',
'meta_key' => 'event_date',
'orderby' => 'meta_value_num',
'order' => 'DESC',
'posts_per_page' => 6,
'paged' => $paged,
'post__not_in' => $exclude_array
);
$temp = $wp_query;
$wp_query = null;
$wp_query = new WP_Query($args);
$default_excerpt_length = 250;
And:
$args = array(
'post_status' => 'publish',
'meta_key' => 'event_date',
'meta_value_num' => time(),
'meta_compare' => '>=',
'orderby' => 'meta_value_num',
'order' => 'DESC',
'posts_per_page' => 6,
'paged' => $paged,
'post__not_in' => $exclude_array
);
$temp = $wp_query;
$wp_query = null;
$wp_query = new WP_Query($args);
$default_excerpt_length = 250;
And:
$today = date('Y-m-d');
query_posts(array(
'post_type' => 'events',
'posts_per_page' => 6,
'paged' => $paged,
'meta_key' => 'event_date',
'orderby' => 'meta_value',
'order' => 'DESC',
'meta_query' => array(
array(
'key' => 'event_date',
'meta-value' => $value,
'value' => $today,
'compare' => '>=',
'type' => 'CHAR'
)
)
));
This is eventually what led to the solution I needed: http://www.advancedcustomfields.com/resources/field-types/date-picker/
I ended up changing the Advanced Custom Field setting to yymmdd as recommend via that url.
This is what I used to query the posts:
And this is what I used to adjust the visual output of the date on the page:
After searching multiple similar posts, I put a solution together from parts of various other SO posts, hopefully it can help somebody else.
I had a custom meta key called “date” (poor naming convention, I know), and this query shows all posts with a custom “date” meta field in the future, sorting by closest to today.