WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am using this php code to display a list of gigs, which is a custom post type. I want to order it by the 'when' value of the post, and omit the gigs that have already happened. How can I modify the following code to get that happening?

$args2 = array(

    'post_type' => 'gig',
    'numberposts' => -1
$gigs = get_posts($args2);
foreach ($gigs as $gig){
    echo '<h2>'.$gig->post_title.'</h2><br />';
    // for each custom fields you want.
    echo get_post_meta($gig->ID,'_gigswhen', true).'</br>';
    echo 'Time: '.get_post_meta($gig->ID,'_gigsstart_time', true).'-'.get_post_meta($gig->ID,'_gigsend_time', true).'<br />';

TO clarify a bit, here is the code in my functions.php

    $config = array(
        'id' => 'gigs-info',
        'title' => 'Gig Info',
        'pages' => array('gig'),
        'context' => 'normal', 
        'priority' => 'high', 
        'fields' => array(), 
        'local_images' => false,
        'use_with_theme' => true //change path if used with theme set to true, false for a plugin or anything else for a custom path(default false).
    $my_meta = new AT_Meta_Box($config);

    //Add fields to your meta box
    $my_meta->addText($prefix.'where',array('name'=> 'Where is the Gig '));
    $my_meta->addDate($prefix.'when',array('name'=> 'When is the Gig '));
    $my_meta->addTime($prefix.'start_time',array('name'=> 'When Does it Start '));
    $my_meta->addTime($prefix.'end_time',array('name'=> 'When Does it End '));
    $my_meta->addText($prefix.'with_who',array('name'=> 'With Who is the Gig '));
    $my_meta->addTextarea($prefix.'words',array('name'=> 'A few words on the gig '));

I'm using a custom meta-box class file that someone kindly pointed me to. So I'm not sure if it's being saved as a unix timestamp or not.. I'm a bit new to wordpress php pogramming :)

share|improve this question

the best way

If you input the gig time as a unix timestamp (using strtotime()), then you can use WP_Query's meta_value_num in the orderby element to set the order. You can also use WP_Query's meta_query to only get posts that have a date that occur in the future.

the other way

If you can't do this for some reason or another (probably that your date is already set), if you get all the gigs, you can reorder the array of posts based on a strtotime() of your meta value. This will be a heavier implementation performance wise, so go with the other one if possible.

share|improve this answer
I edited the question a bit, to show you how the user is inserting the gigs on the backend. I created an options page with custom meta boxes, and used a meta-box-class file that stored it all not sure exactly how the date is being stored? If it helps, when I enter the date, I pick it from a calendar, and it displays as "feb 21, 2012" for example – marctain Feb 29 '12 at 20:08
What happens when you get the meta? What format is it in? I see no reason for my method to not work regardless of data storage method. strtotime() is way flexible. – mor7ifer Feb 29 '12 at 20:15
I figured it out. I just had to add "orderby => date, order => ASC" to the array to get what I wanted. thanks! – marctain Feb 29 '12 at 20:36
That will probably be sorting by the date the gig was posted, not the date listed in the meta...unless you've got some funky hooks and filters on your WP_Query. – mor7ifer Feb 29 '12 at 20:40
Ah you're right actually... I'm not sure how to do what you're suggesting with strtotime(). If you'd like to elaborate I'd be forever grateful! – marctain Mar 2 '12 at 6:30

After a little research, I'm thinking the ability to "unpublish" or expire a post might be more up your alley. A big part of this is that unless your start_time is in Unix format, WordPress won't really know how to interpret it as something comparable to "now". And I'm assuming you're inserting it into something like a MM/DD/YYYY.

Take a look at some of these plugins, maybe that will get you in the right direction. Hopefully at least one has support for Custom Post Types.


share|improve this answer
Expiring the gigs would be only one of the criteria I'm needing. I want to list them on the page by date as well. even just that would be acceptable for now – marctain Feb 29 '12 at 19:33
Storing dates in MM/DD/YYYY format will mess up the sorting. YYYY/MM/DD or timestamp would work. – Stephen Harris Jul 2 '12 at 23:13

Your Answer


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.