In my posts custom fields, I have a meta_key named ending. The value of it is a date in the form of 30-11-2011 (dd-mm-yyyy).

I tried this below but with no luck.


I also tried to do it like below, again with no correct output using my date format

$querystr = "
    SELECT wposts.* 
    FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
    WHERE wposts.ID = wpostmeta.post_id 
    AND wpostmeta.meta_key = 'ending' 
    ORDER BY wpostmeta.meta_value ASC

 $pageposts = $wpdb->get_results($querystr, OBJECT);

 <?php if ($pageposts): ?>
  <?php global $post; ?>
  <?php foreach ($pageposts as $post): ?>
    <?php setup_postdata($post); ?>

      <?php the_title(); ?>
  <?php endforeach; ?>

  <?php else : ?>

 <?php endif; ?>

There are two points I concern.

  1. Is my query_posts correct?
  2. Is the format of the date I type into the custom field, ideal for this?

Thank you for this

1 Answer 1


ad1. meta_values are strings, so they are ordered like strings, not dates. In SQL you should use some casting as a DATE before ordering

ad2. better date format would be yyyy-mm-dd, because sorting this meta field like strings gives the same results as date-type column..

compare order of dates in yyyy-mm-dd


and the same dates in dd-mm-yyyy


both are sorted (ASC), but the first example is in correct order.

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.