1

I know there are a lot of articles and documentation on this but I can't seem to figure it out.

I have a custom post type 'agenda' wich is made for events. I want to sort this by the eventdate. For this, I made a custom field (datepicker) using the ACF plugin.

I've found some documentation on the ACF website wich learned me how to echo the date in dutch format and also an article about sorting the posts by custom meta value. But I can't get it to work.

The key for my custom field is: datum_agenda

Codes I have already tried ( in the $args of the loop):

'orderby' => 'datum_agenda'

&

'meta_key'      => 'datum_agenda',
'orderby'       => 'meta_value_num',
'order'         => 'DESC'

What am I doing wrong here? Seems like my code can't seem to find my meta_key or something because it still sorts by the autovalue.. (the normal post date)

1
  • Thanks @maartje! IF YOU HAVE A DRAG AND DROP POST SORTER INSTALLED, IT OVERRIDES THE DATE SORT! Commented Oct 13, 2021 at 14:43

1 Answer 1

3

Your second approach should in my mind actually work, you do it like this:

Code:

$args = array(
    'post_type' => 'agenda',
    'posts_per_page' => -1,
    'meta_key' => 'datum_agenda',
    'orderby' => 'meta_value_num',
    'order' => 'ASC'
);
$my_query = new WP_Query( $args );

There are two pages at the ACF documentation you might want to read: Date Picker and Order posts by custom fields. Besides that the only thing that comes to mind would be how is the date formated and might the problem originate there, having yymmdd as value for the ACF »Date Picker« field »Save format« is recommended.

4
  • -.- Stupid stupid stupid, I added your code and it still didn't work.. Later I wanted to install a plugin to drag and drop them in a correct order, but I found out it was already installed. That plugin overruled the query. Thanks for your help anyway, but the readon it didn't work was because I had that plugin activated..
    – Maartje
    Commented Feb 5, 2014 at 14:44
  • Yes, beware the quirks coming with some plugins...but on the other hand, you've figured it out now. @Maartje Commented Feb 5, 2014 at 14:46
  • whenever I use 'meta_key', returned list becomes empty Commented Sep 20, 2014 at 9:42
  • @MurhafSousli I don't know what the reason could be. My answer is compliant with the official usage of WP_Query - take a look at Codex: WP_Query - Order + Orderby Parameters. So I'm guessing something else, e.g. some other parameter(s), must be wrong in your setup. Commented Sep 20, 2014 at 11:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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