Title: Incorrect date list order with query_posts
Last modified: August 21, 2016

---

# Incorrect date list order with query_posts

 *  Resolved [bartleriche](https://wordpress.org/support/users/bartleriche/)
 * (@bartleriche)
 * [13 years ago](https://wordpress.org/support/topic/incorrect-date-list-order-with-query_posts/)
 * So I’m printing out all of the posts of the post type ‘agenda_item’. They are
   ordered on the value of a custom field called ‘agenda-datum’. This custom field
   contains a date formatted in the database like this ‘yyyymmdd’.
 * The posts are ordered correctly, accept for these ones:
    20130929 20130825 20131103
 * They should be ordered like this ofcourse:
    20130825 20130929 20131103
 * Strange right? Anyone got an idea why this is happening?
 * Here’s my code:
 *     ```
       <?php
       query_posts( array (
       	'post_type' => 'agenda_item',
       	'meta_key' => more_fields("agenda-datum"),
       	'orderby' => 'meta_value',
       	'order' => 'ASC'
       ) );
   
       while ( have_posts() ) : the_post();
       	// This prints the custom field value of 'agenda-datum'
       	more_fields("agenda-datum");
       endwhile;
       wp_reset_query();
       ?>
       ```
   
 * Thanks!

Viewing 9 replies - 1 through 9 (of 9 total)

 *  [Josh](https://wordpress.org/support/users/josh401/)
 * (@josh401)
 * [13 years ago](https://wordpress.org/support/topic/incorrect-date-list-order-with-query_posts/#post-4016170)
 * Shouldn’t you be “orderby” from the “meta_key”?
 * Like so:
 *     ```
       <?php
       query_posts( array (
       	'post_type' => 'agenda_item',
       	'meta_key' => more_fields("agenda-datum"),
       	'orderby' => 'meta_key',
       	'order' => 'ASC'
       ) );
   
       while ( have_posts() ) : the_post();
       	// This prints the custom field value of 'agenda-datum'
       	more_fields("agenda-datum");
       endwhile;
       wp_reset_query();
       ?>
       ```
   
 *  Thread Starter [bartleriche](https://wordpress.org/support/users/bartleriche/)
 * (@bartleriche)
 * [13 years ago](https://wordpress.org/support/topic/incorrect-date-list-order-with-query_posts/#post-4016172)
 * Hi Josh!
 * Thanks for your help, but when I gave your suggestion a try it didn’t change 
   anything. Still the same problem…
 *  [Josh](https://wordpress.org/support/users/josh401/)
 * (@josh401)
 * [13 years ago](https://wordpress.org/support/topic/incorrect-date-list-order-with-query_posts/#post-4016174)
 * What is the meta key->value pair of one of your custom post types of ‘agenda_item’?
 *  Thread Starter [bartleriche](https://wordpress.org/support/users/bartleriche/)
 * (@bartleriche)
 * [13 years ago](https://wordpress.org/support/topic/incorrect-date-list-order-with-query_posts/#post-4016175)
 * I’m sorry Josh, I don’t get your question. Can you be a little more specific?
   Thanks!
 *  Thread Starter [bartleriche](https://wordpress.org/support/users/bartleriche/)
 * (@bartleriche)
 * [13 years ago](https://wordpress.org/support/topic/incorrect-date-list-order-with-query_posts/#post-4016178)
 * So I refined my code, ‘orderby’ => ‘meta_value_num’,: should work better for 
   numbers… Still doesn’t work right though.. I still get the incorrect order :/
 *     ```
       <?php
       query_posts( array (
       	'post_type' => 'agenda_item',
       	'meta_key' => more_fields("agenda-datum"),
       	'orderby' => 'meta_value_num',
       	'order' => 'ASC'
       ) );
   
       while ( have_posts() ) : the_post();
       	echo get_post_meta( $post->ID, 'agenda-datum', true);
       endwhile;
   
       wp_reset_query();
       ?>
       ```
   
 *  [Josh](https://wordpress.org/support/users/josh401/)
 * (@josh401)
 * [13 years ago](https://wordpress.org/support/topic/incorrect-date-list-order-with-query_posts/#post-4016185)
 * Man, I’m so sorry, but I just received a call and half to jump up and run out.
 * If you are unable to resolve this.. please send me a personal email:
    [http://joshlobe.com/contact-me/](http://joshlobe.com/contact-me/)
 * Please include a link to this thread, so I know who you are. As soon as I return
   home, I’ll be checking up on you 😉
 * Good luck until then!!
 *  Thread Starter [bartleriche](https://wordpress.org/support/users/bartleriche/)
 * (@bartleriche)
 * [13 years ago](https://wordpress.org/support/topic/incorrect-date-list-order-with-query_posts/#post-4016188)
 * Thanks a lot Josh! Anyone else who know what the cause of this bug might be?
 *  [Senff – a11n](https://wordpress.org/support/users/senff/)
 * (@senff)
 * [13 years ago](https://wordpress.org/support/topic/incorrect-date-list-order-with-query_posts/#post-4016210)
 * Did you try using just **agenda-datum** as the meta_key? Or does your plugin 
   require you to use **more_fields(“agenda-datum”)**?
 * I used something like this for a site I recently did and that worked out fine:
 *     ```
       array(
          'post_type'=> 'agenda_item',
          'orderby'  => 'meta_value',
          'meta_key' => 'agenda-datum',
          'order'    => 'asc'
       )
       ```
   
 *  Thread Starter [bartleriche](https://wordpress.org/support/users/bartleriche/)
 * (@bartleriche)
 * [13 years ago](https://wordpress.org/support/topic/incorrect-date-list-order-with-query_posts/#post-4016214)
 * Senff, you just made my day. That did the trick! Thanks so much!

Viewing 9 replies - 1 through 9 (of 9 total)

The topic ‘Incorrect date list order with query_posts’ is closed to new replies.

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 9 replies
 * 3 participants
 * Last reply from: [bartleriche](https://wordpress.org/support/users/bartleriche/)
 * Last activity: [13 years ago](https://wordpress.org/support/topic/incorrect-date-list-order-with-query_posts/#post-4016214)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
