Wordpress: events grouped by month

admin

Administrator
Staff member
Within my WP site I have a category called 'events' where I am publishing event information using two custom fields:

<ol>
<li>eventdate = human readable event date</li>
<li>eventsortdate = YYYY/MM/DD to list events in the correct order.</li>
</ol>

I have this bit of code from a helpful post here: <a href="http://www.davidrisley.com/events-list-with-wordpress/" rel="nofollow noreferrer">http://www.davidrisley.com/events-list-with-wordpress/</a>

Code:
&lt;?php
$timecutoff = date("Y-m-d");
$args = array(
'category_name' =&gt; 'events',
'orderby' =&gt; 'meta_value',
'meta_key' =&gt; 'eventsortdate',
'meta_compare' =&gt; '&gt;=',
'meta_value' =&gt; $timecutoff,
'order' =&gt; 'DESC'
);
$my_query = new WP_Query($args);
if ($my_query-&gt;have_posts()) : while ($my_query-&gt;have_posts()) :
$my_query-&gt;the_post();
$eventdate = get_post_meta($post-&gt;ID, "eventdate", true);
?&gt;
&lt;ul id="events"&gt;
&lt;li&gt;
&lt;strong&gt;&lt;?php echo $eventdate; ?&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;a href="&lt;?php the_permalink() ?&gt;"&gt;&lt;?php the_title(); ?&gt;&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;?php endwhile; else: ?&gt;
&lt;ul id="events"&gt;
&lt;li&gt;&lt;?php _e('No Events Scheduled! Stay Tuned.'); ?&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;?php endif; ?&gt;

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:

Code:
&lt;?php
$timecutoff = date("Y-m-d");
$args = array(
'category_name' =&gt; 'events-press',
'orderby' =&gt; 'meta_value',
'meta_key' =&gt; 'eventsortdate',
'meta_compare' =&gt; '&gt;=',
'meta_value' =&gt; $timecutoff,
'order' =&gt; 'ASC'
);
$my_query = new WP_Query($args);

if ($my_query-&gt;have_posts()) : while ($my_query-&gt;have_posts()) :
$my_query-&gt;the_post();
$eventdate = get_post_meta($post-&gt;ID, "eventdate", true);
?&gt;

&lt;?php if(!isset($currentMonth) || $currentMonth != date("m", strtotime($eventdate))){
    $currentMonth = date("m", strtotime($eventdate));
?&gt;
&lt;li&gt;&lt;?php echo date("m", strtotime($eventdate)); ?&gt;&lt;/li&gt;
&lt;?php
}
?&gt;

&lt;ul&gt;
    &lt;li&gt;
        &lt;h5&gt;&lt;?php echo $eventdate; ?&gt;&lt;/h5&gt;
        &lt;h4&gt;&lt;?php the_title(); ?&gt;&lt;/h4&gt;
        &lt;?php the_content(); ?&gt;
    &lt;/li&gt;
&lt;/ul&gt;
&lt;?php endwhile; else: ?&gt;
&lt;ul id="events"&gt;
&lt;li&gt;&lt;?php _e('No Events Scheduled! .'); ?&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;?php endif; ?&gt;

EDIT: further amendment which functions correctly:

Code:
&lt;?php
$timecutoff = date("Y-m-d");
$args = array(
'category_name' =&gt; 'events-press',
'orderby' =&gt; 'meta_value',
'meta_key' =&gt; 'eventsortdate',
'meta_compare' =&gt; '&gt;=',
'meta_value' =&gt; $timecutoff,
'order' =&gt; 'ASC'
);
$my_query = new WP_Query($args);

if ($my_query-&gt;have_posts()) : while ($my_query-&gt;have_posts()) :
$my_query-&gt;the_post();
$eventdate = get_post_meta($post-&gt;ID, "eventdate", true);
$eventsortdate = get_post_meta($post-&gt;ID, "eventsortdate", true);
?&gt;

&lt;?php if(!isset($currentMonth) || $currentMonth != date("m", strtotime($eventsortdate))){
    $currentMonth = date("m", strtotime($eventsortdate));
?&gt;
&lt;li&gt;&lt;?php echo date("F", strtotime($eventsortdate)); ?&gt;&lt;/li&gt;
&lt;?php
}
?&gt;

&lt;ul&gt;
    &lt;li&gt;
        &lt;h5&gt;&lt;?php echo $eventdate; ?&gt;&lt;/h5&gt;
        &lt;h4&gt;&lt;?php the_title(); ?&gt;&lt;/h4&gt;
        &lt;?php the_content(); ?&gt;
    &lt;/li&gt;
&lt;/ul&gt;
&lt;?php endwhile; else: ?&gt;
&lt;ul id="events"&gt;
&lt;li&gt;&lt;?php _e('No Events Scheduled! .'); ?&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;?php endif; ?&gt;