i have a custom post type 'events'. Each event (post) can have multiple dates. The dates (date, time, location) are stored as array. I need a query that creates a chronological list of all events from the meta data (events can occur multiple times).

The custom-meta-box looks like this:

Add Dates

The resulting database entry in wp_postmeta has the meta_key '_events_termine' with the following meta_value (using WPAlchemy):


I display the dates for a single event with the following code:

    <?php global $events_meta_termine;
    $meta = get_post_meta(get_the_ID(), $events_meta_termine->get_the_id(), TRUE);
    foreach ($meta['termin_group'] as $termin)
    { ?>
      <td><?php echo mysql2date('D', $termin['termin_date'], true); ?></td>
      <td><?php echo mysql2date('j. F Y', $termin['termin_date'], true); ?></td>
      <td><?php echo $termin['termin_time']; ?> Uhr</td>
      <td><?php echo $termin['termin_location']; ?></td>
    <?php } ?>

The output looks like this:


So everything works fine except i have no idea how to create a page that lists all events (title, date, location) in chronological order where a single event can occur several times.

Any help is highly appreciated!

(i'm sorry i asked a similar question before, but i came to a dead end and had to start over.)

  • At what URL do you want to display these? Using what WordPress theme template file? How does $events_meta_termine get its value exactly? Can you update the code in your question to show that? – MikeSchinkel Mar 12 '11 at 19:46
  • @MikeSchinkel: I create a page with a custom page template to display the list. It is a playing schedule for a theatre. Productions play on several dates. – Christoph Mar 13 '11 at 15:48
  • If you're having trouble, you should update the previous question, not create a new one: wordpress.stackexchange.com/questions/10328/… – scribu Mar 13 '11 at 19:44
  • @scribu - Sorry, i thought because the previous question already had an accurate answer it would be better to create a new one. I tried to break my problem into pieces but in the end the pieces didn't fit together. So I started over and finished everything except this one output. Now, having the data as array seemed to me like a different approach... – Christoph Mar 13 '11 at 22:58
  • Fair enough. – scribu Mar 13 '11 at 23:29

Here's the code that finally worked for me:

global $events_meta_termine;
$today = getdate();
$my_query = new WP_Query('post_type=events&posts_per_page=-1&monthnum='.$today["mon"]);
while ($my_query->have_posts()) : $my_query->the_post();
    $do_not_duplicate = $post->ID;
    $meta = get_post_meta(get_the_ID(), $events_meta_termine->get_the_id(), TRUE);
    foreach ($meta['termin_group'] as $termin)

usort($events, "cmp"); 
function cmp($a, $b){ 
return strcmp($a['date'],$b['date']); 
} ?>

<?php $current_month='';
 foreach ($events as $event){   
     if ($current_month!=mysql2date('F Y', $event['date'], true))
         $current_month=mysql2date('F Y', $event['date'], true);
         echo '<tr><td colspan="5">'.$current_month.'</td></tr>';
     } ?>
      <td><?php echo $event['title']; ?></td>
      <td><?php echo mysql2date('D', $event['date'], true); ?></td>
      <td><?php echo mysql2date('j. F Y', $event['date'], true); ?></td>
      <td><?php echo $event['time']; ?> Uhr</td>
      <td><?php echo $event['location']; ?></td>
<?php } ?>


To create a main page which lists all (page > events) ... you would need to create your own "Loop" ... see the codex for The Loop ...

Once you've done that, then the code you have above could be modified to something like the following:

global $events_meta_termine;


while ($my_query->have_posts())

    // echo page stuff

    $meta = $events_meta_termine->the_meta(get_the_ID());

    foreach ($meta['termin_group'] as $termin) 
        // echo event stuff (for the current page)


Even though the example above would allow you to list all the events on a single page, it wouldn't be chronological ... you may need to do some filtering of the data prior to listing it on the page, e.g. constructing a custom array which takes into account the pages and each of the events per page and their dates. You would need to key off of the date and do some sorting.

  • Thanks very much for your answer. That's exactly where my programming skills come to an end rather quickly. Maybe i can get further with your hint about the custom array prior to listing. Thank you! – Christoph Mar 13 '11 at 23:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.