Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have a calendar plugin that uses custom posts types to create events. There are several custom fields that allow me to create various and seemingly unlimited post queries. The one I'm currently focus on is the field containing the "event start date", titled = evcal_start_date.

What I need to do is display the next upcoming event from today, and this has become a bit of a challenge.

Current WP_query;

<?php
    $currentdate = date("m/d/Y",mktime(0,0,0,date("m"),date("d"),date("Y")));

    $my_query = new WP_Query(  array ('showposts' => 1,
    'post_type' => 'ajde_events',
    'meta_query'=> array(
    array(
    'key' => 'evcal_start_date',
    'meta_compare' => '>',
    'value' => $currentdate,
    'type' => 'DATE',
    )),
    'meta_key' => 'evcal_start_date',
    'orderby' => 'meta_value',
    'order' => 'ASC'
    )
    );

    while ($my_query->have_posts()) : $my_query->the_post();
    $title = get_the_title();
    $date = get_post_meta($post->ID, 'evcal_start_date', true);

    endwhile; 
?>
<div class="featuredtitle"><?php echo $title; ?><br />
<?php echo $date; ?></div>

I've made sure that the formatting of $currentdate matches that of evcal_start_date. It is stored as "5/25/2012".

I can't guarantee my client will always create posts in order and that is why it's imperative to use the custom field of the event's start date.

I would really appreciate any help. Have a wonderful Memorial Day weekend!

share|improve this question

1 Answer 1

To sort based on a date field, you need to convert the date to a UNIX datetime format. It's probably best to store the date in a separate custom field. I've never had to set this up, but you'd

  1. hook into save_post
  2. look for a custom field with the date in it
  3. convert that to a datetime in a different custom field (probably started with an underscore to hide it from the custom fields interface).
  4. Query for the hidden custom field and use it to sort.

There are also some custom meta box plugins that come with a date picker and the ability to store UNIX format dates.

To do the human-readable date to UNIX conversion, you'll want to learn about PHP's strtotime() function. To convert back (if you're storing the UNIX date and want to print it in a human-readable format), you'll use the date() function.

share|improve this answer
    
If I can modify the plugin's storage of the custom field that'd be great solution, but I'm weary of altering plugins too much. Is there anyway of doing this within the query? –  frankV May 25 '12 at 17:57
    
I've never tried this and don't have time to code a whole plugin right now (sorry, on another I'd go for it), but you should be able to write a separate plugin that checks for the custom value saved by the plugin and converts it. Maybe someone else has a better idea on how to solve this too! –  mrwweb May 25 '12 at 18:59
    
There's no real need to re-code the plugin or a new one. Your tip seems to work very well. I used a different method of strtotime() and I'll share my findings in the morning –  frankV May 26 '12 at 4:13

Your Answer

 
discard

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.