Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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):

a:1:{s:12:"termin_group";a:2:{i:0;a:3:{s:11:"termin_date";s:10:"2011/03/14";s:11:"termin_time";s:5:"19.30";s:15:"termin_location";s:15:"Max-Reger-Halle";}i:1;a:3:{s:11:"termin_date";s:10:"2011/03/15";s:11:"termin_time";s:5:"20.00";s:15:"termin_location";s:15:"Max-Reger-Halle";}}}

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

<table>
    <?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)
    { ?>
    <tr>
      <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>
    </tr>
    <?php } ?>
</table>

The output looks like this:

Output

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.)

share|improve this question
    
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
add comment

2 Answers

Christoph,

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;

$my_query = new WP_Query([GET-ONLY-THE-PAGES-THAT-USE-THE-META-BOX]);

while ($my_query->have_posts())
{
    $my_query->the_post();

    // 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)
    }
}

REVISION

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.

share|improve this answer
    
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
add comment
up vote 0 down vote accepted

Here's the code that finally worked for me:

<?php 
global $events_meta_termine;
$today = getdate();
$my_query = new WP_Query('post_type=events&posts_per_page=-1&monthnum='.$today["mon"]);
$events=array();
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)
    { 
        $event=array();
        $event['title']=get_the_title();
        $event['date']=$termin['termin_date'];
        $event['time']=$termin['termin_time'];
        $event['location']=$termin['termin_location'];
        $events[]=$event;
    } 
endwhile; 
wp_reset_query();

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

<table>
<?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>';
     } ?>
    <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>
    </tr>
<?php } ?>
</table>
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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