I am making an event sidebar section that will only display the next 3 events. I have got the custom post type and custom fields all working but I can seem to figure out how to order the posts by the start date of the events, which is a custom field value. Is there a php function that can compare dates and organize them into a certain order. I think it would also have to hold the post-id with the newly arranged dates so that when I read through the values, i can display the appropriate post with that date.
Does anyone have a certain direction to steer me in?
I think this is what I need to do:
- Read through the posts and grab the dates
- Sort the dates with the post-id associated with those dates
- Read through the sorted dates and re-display the first 3 posts by post-id
I get lost on how to code that though... This is what I have so far. This code just displays them by their publish dates in wordpress.
<?php query_posts('post_type=events');
if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php $dateStart = get_post_meta($post->ID, 'date-start', true);?>
<div class="date"><?php echo $dateStart; ?></div>
<?php endwhile; endif; wp_reset_query(); ?>