Question

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

Was it helpful?

Solution

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>

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top