WordPress.org

Ready to get started?Download WordPress

Forums

custom field date used two ways, one for sorting, one for display (3 posts)

  1. zandercent
    Member
    Posted 3 years ago #

    Hi,

    I'm using a custom field called 'date' as place for the client to select a date for events, then using a query to sort the posts so they appear in date order on the front end.

    the query is... <?php query_posts('cat=3&showposts=5&order=asc&orderby=meta_value&meta_key=date'); $i=0; ?>

    now to sort the posts correctly the date format needs to be yyyy-mm-dd,
    I'd like to also display the contents of the custom field on the front end of the site as dd-mm-yyy (as people are used to reading it in the UK) or, even better, as a fully written out date (eg 9th february 1980)

    Anyone know any sneaky way to do this without the client haing to select the date twice in 2 seperate custom fields?

    Thanks team!

  2. zandercent
    Member
    Posted 3 years ago #

    could an administrator please move this to the hacks sub-forum?

  3. zandercent
    Member
    Posted 3 years ago #

    Found an anwser - for whoever is interested

    you can create your custom field (eg date) to input dates in reverse order (yyyy/mm/dd) so they can be sorted numerically (ie in date order)

    You can then display this date in a more friendly way on the front end like this.

    <?php
    $date = get_post_meta($post->ID, 'date', true);
    if ($date){
    $mydate = "$date";
    echo date('l, j F, Y', strtotime($mydate));
    
    }
    ?>

Topic Closed

This topic has been closed to new replies.

About this Topic