I looked around for a way to query AFC sub fields and I finnaly found a way. This is mostly for date fields, but I guess you can use this example for other
This will search the post type
events for the field dates
which has a sub field called when
.
$field = 'dates_0_when';
$events_query = new WP_Query( [
'posts_per_page' => -1,
'post_type' => 'events',
'order' => 'ASC',
'orderby' => 'meta_value',
'meta_key' => $field,
'meta_type' => 'DATETIME'
] );
This displays how to search for posts between two date intervals:
// find date time now
$date_now = date('Y-m-d H:i:s');
$time_now = strtotime($date_now);
// find date time 365 days from now
$time_next_week = strtotime('+365 day', $time_now);
$date_next_week = date('Y-m-d H:i:s', $time_next_week);
$field = 'dates_0_when';
$events_query = new WP_Query( [
'posts_per_page' => -1,
'post_type' => 'events',
'meta_query' => array(
array(
'key' => $field,
'compare' => 'BETWEEN',
'value' => array( $date_now, $date_next_week ),
'type' => 'DATETIME'
)
),
'order' => 'ASC',
'orderby' => 'meta_value',
'meta_key' => $field,
'meta_type' => 'DATETIME'
] );
This page has been requested 3872 times