Hello
Within my WP site I have a category called 'events' where I am publishing event information using two custom fields:
- eventdate = human readable event date
- eventsortdate = YYYY/MM/DD to list events in the correct order.
I have this bit of code from a helpful post here: http://www.davidrisley.com/events-list-with-wordpress/
<?php
$timecutoff = date("Y-m-d");
$args = array(
'category_name' => 'events',
'orderby' => 'meta_value',
'meta_key' => 'eventsortdate',
'meta_compare' => '>=',
'meta_value' => $timecutoff,
'order' => 'DESC'
);
$my_query = new WP_Query($args);
if ($my_query->have_posts()) : while ($my_query->have_posts()) :
$my_query->the_post();
$eventdate = get_post_meta($post->ID, "eventdate", true);
?>
<ul id="events">
<li>
<strong><?php echo $eventdate; ?></strong><br />
<a href="<?php the_permalink() ?>"><?php the_title(); ?></a>
</li>
</ul>
<?php endwhile; else: ?>
<ul id="events">
<li><?php _e('No Events Scheduled! Stay Tuned.'); ?></li>
</ul>
<?php endif; ?>
This will ensure events are listed in the correct order. However, I would also like to group events by month - so have a header of "month" and group all events which fall into that month display under that title.
Any ideas much appreciated, or alternative suggestions for how to achieve this also much appreciated. Thanks.
EDIT:
Amended code taking into account suggested code:
<?php
$timecutoff = date("Y-m-d");
$args = array(
'category_name' => 'events-press',
'orderby' => 'meta_value',
'meta_key' => 'eventsortdate',
'meta_compare' => '>=',
'meta_value' => $timecutoff,
'order' => 'ASC'
);
$my_query = new WP_Query($args);
if ($my_query->have_posts()) : while ($my_query->have_posts()) :
$my_query->the_post();
$eventdate = get_post_meta($post->ID, "eventdate", true);
?>
<?php if(!isset($currentMonth) || $currentMonth != date("m", strtotime($eventdate))){
$currentMonth = date("m", strtotime($eventdate));
?>
<li><?php echo date("m", strtotime($eventdate)); ?></li>
<?php
}
?>
<ul>
<li>
<h5><?php echo $eventdate; ?></h5>
<h4><?php the_title(); ?></h4>
<?php the_content(); ?>
</li>
</ul>
<?php endwhile; else: ?>
<ul id="events">
<li><?php _e('No Events Scheduled! .'); ?></li>
</ul>
<?php endif; ?>
EDIT: further amendment which functions correctly:
<?php
$timecutoff = date("Y-m-d");
$args = array(
'category_name' => 'events-press',
'orderby' => 'meta_value',
'meta_key' => 'eventsortdate',
'meta_compare' => '>=',
'meta_value' => $timecutoff,
'order' => 'ASC'
);
$my_query = new WP_Query($args);
if ($my_query->have_posts()) : while ($my_query->have_posts()) :
$my_query->the_post();
$eventdate = get_post_meta($post->ID, "eventdate", true);
$eventsortdate = get_post_meta($post->ID, "eventsortdate", true);
?>
<?php if(!isset($currentMonth) || $currentMonth != date("m", strtotime($eventsortdate))){
$currentMonth = date("m", strtotime($eventsortdate));
?>
<li><?php echo date("F", strtotime($eventsortdate)); ?></li>
<?php
}
?>
<ul>
<li>
<h5><?php echo $eventdate; ?></h5>
<h4><?php the_title(); ?></h4>
<?php the_content(); ?>
</li>
</ul>
<?php endwhile; else: ?>
<ul id="events">
<li><?php _e('No Events Scheduled! .'); ?></li>
</ul>
<?php endif; ?>