Hello,
I’m trying to order my custom post type ‘training’ by my ACF date field ‘training__date’. This is how my code looks like:
$today = date('Ymd');
// Arguments for the query
$args = array(
'post_type' => 'training',
'posts_per_page' => -1,
'post_status' => 'publish',
'meta_query' => array(
array(
'key' => 'training__date',
'value' => $today,
'type' => 'DATE',
'compare' => '>='
)
),
'meta_key' => 'training__date',
'order_by' => 'meta_value_num',
'order' => 'ASC',
);
$training_query = new WP_Query($args);
The strange thing is that the meta_query is working, but the ordering by date isn’t. Am I doing something wrong? I also tried changing the order_by value to meta_value instead of meta_value_num, but that didn’t make a difference.
The return format of the date field is Ymd.
WP version: 4.8
ACF PRO 5.5.14
Thanks in advance.
I have the same issue – also trying to order by a custom date field. So the code looks almost identical. I have orderby written correctly but still have this issue.
I’ve also tried ‘orderby’ => ‘meta_value’ and ‘orderby’ => ‘meta_value_num’ with no difference.
My args are:
$args = array(
‘post_type’ => array( $post_type ),
‘post_status’ => array( ‘publish’ ),
‘posts_per_page’ => -1,
‘tax_query’ => array(
array (
‘taxonomy’ => $taxonomy,
‘terms’ => $term_id,
)
),
‘meta_query’ => array(
array(
‘key’ => ‘date’,
‘compare’ => ‘>=’,
‘value’ => $date_now,
‘type’ => ‘DATETIME’
),
),
‘order’ => ‘DESC’,
‘orderby’ => ‘meta_value’,
‘meta_key’ => ‘date’,
‘meta_type’ => ‘DATETIME’,
);
I’d be grateful if you have any pointers 🙂
Okay – I tried this again. ‘orderby’ => ‘meta_value_num’ solved the issue. As long as the date format for this field is set to YYYYMMDD
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!
Are you building WordPress sites with ACF and @BeaverBuilder, and wanted to use your ACF Blocks in both the block editor and Beaver Builder?
— Advanced Custom Fields (@wp_acf) May 10, 2023
The BB team recently added support for using ACF Blocks in Beaver Builder. Check it out 👇https://t.co/UalEIa5aQi
© 2023 Advanced Custom Fields.
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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.