Home › Forums › General Issues › Custom query filtering & ordering by ACF fields
I am trying to create a custom query in my template file that filters posts by whether the end_date ACF is after today (event has not ended yet), and then order the output by the value in the event_start ACF. But I can’t seem to get my query right:
$today = date('Ymd');
$thePosts = new WP_Query(array(
'post_type' => 'post',
'posts_per_page' => 10,
'meta_key' => 'event_start',
'meta_query' => array(
array(
'key' => 'event_end',
'value' => '".$today."',
'compare' => '>'
)
),
'order' => 'DESC',
'orderby' => 'meta_value',
'post_status' => 'publish',
));
'meta_query' => array(
array(
'key' => 'event_end',
'value' => $today, // change this
'compare' => '>'
)
),
John,
I made this change, but am still not getting any results. I have one Event item that is set for the end of this year, so it should show up.
Are you using a date field or a date/time field?
Is there a value in both fields?
The two fields are Date Picker fields. Both field have a value.
Honestly, I don’t know why it’s not working, unless it has something to do with the mix of meta settings. Try this
$today = date('Ymd');
$thePosts = new WP_Query(array(
'post_type' => 'post',
'posts_per_page' => 10,
'meta_query' => array(
array(
'key' => 'event_end',
'value' => '".$today."',
'compare' => '>'
),
'date_clause' => array(
'key' => 'event_start'
'compare' => 'EXISTS'
)
),
'orderby' => array('date_clause' => 'DESC'),
'post_status' => 'publish',
));
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.