2

I've read alot, I've tried a bunch of different things, I still can't get my query to order posts by acf's datepicker field. This is what my code looks like:

$args = array(
    'post_type'   =>  'arm_careers',
    'post_status' => 'publish',
    'posts_per_page' => -1,
    'meta_key' => 'post_date',
    'orderby' => 'meta_value_num',
    'meta_type' => 'DATE',
    'order' => 'ASC',
);

I'm not using PRO, I'm using the free one. The save format is yymmdd, I've also tried YYYYMMDD but neither worked.

Here is what it currently looks like: August 14, 2018 August 2, 2018 August 9, 2018

when I want it to look like: August 2, 2018 August 9, 2018 August 14, 2018

The display format is MM d, yy.

2

You need to convert your saved format to the post_date format which is in

Y-m-d H:i:s

Only then will the comparators work properly with automatically generated query objects.

  • 1
    Change orderby=> 'meta_value_num' to 'meta_value' – Nicholas Koskowski Aug 2 '18 at 19:18
1

ACF stores dates as yyyymmdd in meta values, so it's very probable that casting it to DATE won't work properly...

And there is no need to compare it as number - dictionary sorting will work here.

Try this:

$args = array(
    'post_type' => 'arm_careers',
    'post_status' => 'publish',
    'posts_per_page' => -1,
    'meta_key' => 'post_date',
    'orderby' => 'meta_value',
    'order' => 'ASC',
);

PS. I'm pretty sure that you can't set the format that ACF will use to store dates. You can set the format for showing these values or for getting them.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.