Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

query_posts("paged=$paged&meta_key=ending&order_by=meta_value&order=DESC")

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(); ?>
<br>
  <?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
add comment

1 Answer

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

2010-03-06
2012-06-05

and the same dates in dd-mm-yyyy

05-06-2012
06-03-2010

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

share|improve this answer
add comment

Your Answer

 
discard

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.