I'm trying to sort custom post type made with ACF plugin by most current date. Ideally future most current post should be first. Currently, I'm sorting by date, which gives me the earliest post as the first post.
Here is my code.
$loop = new WP_Query( array(
'post_type' => 'events',
'posts_per_page' => -1,
'meta_key' => get_sub_field( 'date_start' ),
'orderby' => 'meta_value_num',
'order' => 'ASC'
) );
Here is the screenshot of my website to make things clear.
I have tried this post to no avail https://stackoverflow.com/questions/28951370/sort-wp-query-by-acf-datepicker
It seams that my meta_query
is not properly setup.
This is the meta_query
that I tried.
'meta_query' => array(
array(
'key' => get_sub_field( 'date_start' ),
'compare' => '>=',
'value' => current_time('Ymd'),
)
),
note: for some reason I have to do get_sub_field('date_start')
plain 'date_start'
doesn't return any results.
date_start
a sub-field of a repeater or group field? Also,get_sub_field()
andget_field()
return the value for the current post. It doesn't make any sense to use it as the key.date_start
belongs to a group. This totally make sense, but for some odd reason I can't get any result without usingget_sub_field()
.get_sub_field()
, it's almost certainly incorrect, and by coincidence. Ifdate_start
is the sub field of a group, then the meta key will not bedate_start
, it will be prefixed with the group's key.