using three custom fields for a date, but how use these for "orderby"?!


I have three custom meta fields in a custom post type, one for date, one for month, and one for year. The two first selectable by a drop-down, the last a normal textfield.

Earlier I have used one text field, where the user had to write the date as “YYYYMMDD” (but this was obviously asking too much…:wink:, and then used this to order by using metakey/value and so on.

So… is there any way I can use those three new fields to order by instead?

  • torsteino
    • WPMU DEV Initiate

    Yes, I’m using a post type to make a calendar, so I have to sort the posts according to the date for the events.

    Here is how I used to do it earlier:

    // Fetch posts from calendar
    $today = date("Ymd") ;
    query_posts("post_type=calendar&orderby=meta_value&meta_key=sort&order=ASC&meta_compare=>=&meta_value=$today&posts_per_page=99"); ?>
    <?php while (have_posts()) : the_post(); ?>
    <?php $custom = get_post_custom($post->ID);
    $date = $custom["date"][0];
    $location = $custom["location"][0];
    $sort = $custom["sort"][0];?>
    <li class="calendar">
    <a href="<?php the_permalink()?>" class="calendar">
    <h2 class="kalender-dato"><?php the_title(); ?></h2>
    <p class="kalender-sted"><?php echo $date; ?>, <?php echo $location; ?></p>
    <?php endwhile;?>

    The custom field “sort” was simply the date written as YYYYMMDD (which the user had to do “manually”, and the “date” field was used to write the date in a more friendly way than YYYYMMDD (but as you see, it was just used as text, not for sorting in any way).

    With custompress it was simple to set up dropdowns to select the date, but then I get THREE meta values, not one. If I set up the CPT in functions.php I think I should manage to “merge” the three values, so I would get 20121123 instead of 2012, 11 and 23 as seperate values, before I store them as a meta value, but I really dont know how I would do that using custompress?

    Of course, it would also be nice to keep those three values stored seperatly, in addition to merged, but that shouldn’t really be a problem I think?

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.