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!
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
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 thedate()
function.