Search code examples
wordpressadvanced-custom-fields

Wordpress: WP_Query ordering anomaly


I'm ordering a set of posts by an ACF date field using these arguments in WP_Query

'orderby' => 'meta_value',
'order' => 'ASC',
'meta_key'      => 'date', //ACF date field

Which seems to work, except it's throwing an anomaly.

This is an echo of the list of dates (stored as Ymd)

20211221
20220118
20220125
20220201
20220208
20220215
20220222
20220301
...
20221122
20221129
20221206
20221213
20221220
20220111

The last one, which should be second, is appearing last. (they are weekly dates starting 21st December 2021


Solution

  • Try to use meta_value_date instead of meta_value as orderby parameter.

    see also https://developer.wordpress.org/reference/classes/wp_query/#order-orderby-parameters