Sorting a query Field by date

I have my site, which at the moment is display university open-days, at the moment they are sorting by order of page title, when actually I would prefer they sorted in order of date, soonest first, latest last.

Below is the code that i have so far and I cannot seem to work out how to sort them in order of date, any help?

<?php query_posts('post_type=page&post_parent=2&posts_per_page=-1&orderby=title&order=ASC'); ?>
<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>

<!-- Calender Items -->

<?php $key = 'next_open_day'; $themeta = get_post_meta($post->ID, $key, TRUE); if($themeta != '') { 

echo '<div class="calender-item">';
echo '<h2>';
echo '<a href="';
the_permalink();
echo '">';
the_title();
echo '</a>';
echo '</h2>';
echo '<p>';
the_field('next_open_day');
echo '</p>';
echo '<p>';
the_field('open_day_description');
echo '</p>';
echo '<a target="_blank" href="';
the_field('university_web_address');
echo '">';
echo 'Visit Website';
echo '</a>';
echo '</div>';

} ?>    

<!-- END OF CALENDER ITEM -->   

<?php endwhile; ?>
<?php wp_reset_query(); ?>

Related posts

Leave a Reply

1 comment

  1. &orderby=date will return posts by their dates.

    <?php query_posts('post_type=page&post_parent=2&posts_per_page=-1&orderby=date&order=ASC'); ?>
    

    Replace first line with this to show post according to their date.

    Reference – Passing variables to query_posts


    Update –

    To sort the posts as per custom field value, Here’s the new code.

       query_posts('post_type=post&posts_per_page=-1&orderby=meta_value&meta_key=next_open_day&order=ASC'); ?>
    

    Note – The date should be saved in – YYYY-MM-DD format. However we can further convert that date into required format. E.g. –

    <?php
    $date = "2012/08/21"; 
    list($y, $m, $d) = split('/', $date); 
    $newdate = $d.'/'.$m.'/'.$y;
    echo $newdate;
    ?>