PLEASE NOTE: These forums are no longer utilized and are provided as an archive for informational purposes only. All support issues will be handled via email using our support ticket system. For more detailed information on this change, please see this blog post.

Sorting By Date Entered Into Custom Field

  1. Chris Higgins
    Member

    I'm in the process of building an online events calendar that a number of different people can contribute events to, and I'm looking for some help regarding sorting the posts in the order of the events, rather than in the WP standard order that they were published in.

    I've built a front-end Gravity Form that users can enter information about their event into, and then submit as a WP post, which is then approved and published by me. One of the fields that the user is required to fill in is a date custom field (the date of their event), and it's this value that I want to sort the posts by.

    At the moment, I'm using the following code to sort the posts:

    <?php query_posts($query_string . '&meta_key=event-date&orderby=meta_value&order=asc' );  ?>
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>

    I came up with this code for a previous project, however there was a bit of a quirk which meant the user not only had to fill in the date of the event, but also a "sort value", which was the date of the event written as YYYYMMDD. The posts were then sorted by this sort value. I would like to avoid using this technique on this project, as I think it complicates the user experience a great deal.

    The website is for a UK audience, so I'd like to use the DDMMYYYY format for the event date if possible. The code I posted above appears to be working, however it seems to be sorting the posts as if the date were a numeric value (01062013), rather than a date value (01/06/2013).

    eg. An event posted for July 16th (16/07/2013) comes before a June 20th event (20/06/2013), when the June event should be displayed first. I think this is because when you convert these dates to numeric values, the June event (20062013) is a greater number than the July event (16072013).

    If anyone can offer any advice on how to make this work properly, I'd really appreciate it. Hope everything I've written makes sense. If you need further clarification, please let me know.

    Cheers,

    Chris

    Posted 11 years ago on Monday April 22, 2013 | Permalink