I have a custom post type of films using ACF (Advanced Custom Fields) to store the date, time and location in separate custom fields. I need to display a grid where the posts are sorted in a grid for each day and each day should have columns of venues and rows of times.
If you go to http://vff.vibethink.net/schedule/ and click on view as grid you will see what is needed. So far I have everything working, but I can’t figure out how to loop through the posts so that events with the same time are displayed on the same row.
Right now I’m looping through each venue on each row and if the post equals the column venue inject it, if not create a blank cell.
The posts are sorting properly, I’m just not sure how to loop through and open and close the time rows and insert the correct number of blank cells between posts so the table format isn’t broken.I’m sure there is some way I can work it with a series of counters while working through the loop, but I may need to change the way I call the posts completely. Any help I can get would be appreciated.
The code I have so far is:
<?php
$day_check = '';
$start_check = '';
$args=array(
'post_type' => 'films',
'meta_query' => array(
'relation' => 'AND',
array( 'key' => 'showing_date', 'compare' => 'LIKE' ),
array( 'key' => 'starting_time', 'compare' => 'LIKE' ),
array( 'key' => 'location', 'compare' => 'LIKE' )
),
'meta_key' => 'showing_date',
'orderby' => 'meta_value',
'order' => 'ASC',
'posts_per_page' => -1,
'posts' => -1
);
add_filter('posts_orderby','orderbyreplacelocation');
$schedule_query = null;
$schedule_query = new WP_Query($args);
while ($schedule_query->have_posts()) : $schedule_query->the_post();
$day = get_field('showing_date');
$start = get_field('starting_time');
$field_key = "field_5419aa1a6dd4c";
$field = get_field_object($field_key);
$fieldvalue = get_field('location');
$label = $field['choices'][$fieldvalue];
$time = get_field('starting_time');
$time_format = DATE("g:i a", STRTOTIME($time));
$term_list = wp_get_post_terms($post->ID, 'filmcategory', array("fields" => "ids"));
$name_list = wp_get_post_terms($post->ID, 'filmcategory', array("fields" => "names"));
$genre_list = wp_get_post_terms($post->ID, 'genre', array("fields" => "slugs"));
if ($day != $day_check) {
if ($day_check != '') {
echo '</div></div>'; // close the list here
}
$date = DateTime::createFromFormat('Ymd', get_field('showing_date'));?>
<div class="schedule-day">
<div class="schedule-date" data-day="<?php echo $date->format('l');?>">
<?php echo $date->format('l, F j');?>
</div>
<div class="day-grid">
<div class="grid-row day-head">
<div class="time-grid cell"></div>
<?php if( $field )
{
$toSort = $field['choices'];
$gridVenues = ARRAY ( "Culbreth-Theater","Newcomb-Hall","PVCC-Dickinson-Center","Paramount","Regal-1","Regal-2","Regal-3","Regal-4" );
ksort($toSort);
foreach( $toSort as $k => $v )
{
if (in_array($v, $gridVenues)){
$venue = substr($k, strpos($k, "-") + 1);
echo '<div class="location-grid cell">'.$venue.'</div>';
}
}
}?>
</div>
<?php }?>
<div class="grid-row">
<div class="time-grid cell">
<?php echo $time_format; ?>
</div>
<?php
$i = 0;
foreach( $toSort as $k => $v )
{
if (in_array($v, $gridVenues)){
if(get_field('location') == $k){ ?>
<div class="location-grid cell event-cell">
<a href="<?php the_permalink();?>">
<?php the_title();?>
</a>
<?php
$thumb = wp_get_attachment_image_src( get_post_thumbnail_id(), 'thumbnail' );
$url = $thumb['0'];
?>
<div class="tooltip">
<div class="tool-head">
<picture>
<a href="<?php the_permalink();?>">
<img src="<?php echo $url;?>">
</a>
</picture>
<div class="tool-head-content">
<h5><a href="<?php the_permalink();?>"><?php the_title(); ?></a></h5>
<span class="showing"><?php echo $date->format('D');?>, <?php echo $time_format;?></span><br/>
<span class="location"><?php the_field('location');?></span>
<span class="tags"><?php foreach ($name_list as $term) { echo $term; ?>, <?php }?></span>
</div>
<div class="clear"></div>
</div>
<div class="tool-main">
<a class="more" href="<?php the_permalink();?>">Read More</a>
<a class="button buy_button film_buy_button" data-fancybox-type="iframe" href="<?php the_field('ticket_link');?>">Buy Tickets<i class="fa fa-angle-double-right"></i></a>
</div>
</div>
</div>
<?php }
else
{ ?>
<div class="location-grid cell">
</div>
<?php }
}
}?>
</div>
<?php
$day_check = $day;
$start_check = $start;
endwhile; wp_reset_postdata(); remove_filter('posts_orderby','orderbyreplacelocation');?>
</div>
</div>
</div>