views:

31

answers:

1

Hello

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

  1. eventdate = human readable event date
  2. 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; ?>
+1  A: 

You can do amazing things with the WP_Query() function, but I think gruoping doesn't belongs to it. What you could do is building yourself an array and outputting the results from that array. Or you can just save the current month and output the next month as soon as it changes:

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

This will print put the month number for the first event ($surrentMonth has not been set, yet) and then again every time a new month is present.

But for sure you have to change the output (the LI) for what you want it to be.

Kau-Boy
Thanks for this - all I get is an "01" displayed at the top of the page though. I've posted my revised code above, any ideas why this produces that result?
Dave
The date('m') was just an exmaple. Using 'm' will output the number of the month, whereas 'F' will output the name of the month. See the php doc here: http://www.php.net/manual/de/function.date.php
Kau-Boy
Hey, thanks a lot. With a bit of tweaking I've got it to work. Many thanks!! (see above tweaked code)
Dave