WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

share|improve this question
up vote 1 down vote accepted

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.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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