Sorting and displaying posts in a grid format using WP and ACF

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>

So you want a grid…

______ Venue 1 Venue 2 Venue 3 …
8:00
8:15
8:30
etc.

so… sorted by Time, then Venue;

Output Venue Header Row
Set Current_Time to Null
Foreach Entry
  If This_Entry_Time != Current_time
    Start New Row;
    Set Current_Time to This_Entry_Time
    Set Current_Venue to Null
  EndIf
  If This_Entry_Venue != Current_Venue
    Start new Cell
    Set Current_Venue to This_Entry_Venue
  EndIf
  Echo Appropriate Data (This_Event_Name)
EndForeach

EDIT: That doesnt take into account empty venues on a given time. Hmm. Okay. Slight change.

Define Venue Array.
Output Venue Header Row
Set Current_Time to Null
Set Current_Venue_Id to count(Venue_Array) - 1.
Foreach Entry
  If This_Entry_Time != Current_time
    Add blank cells equal to count(Venue_Array) - 1 - Current_Venue_Id
    Start New Row; 
    Add time-display cell (Do Not terminate cell.)
    Set Current_Time to This_Entry_Time
    Set Current_Venue to Null
    Set Current_Venue_Id to 0
  EndIf
  If This_Entry_Venue != Current_Venue
    Terminate current cell.
    Find position of new Venue in array.
    Create blank cells equal to New_Venue_Id - Current_Venue_Id
    Start new Cell for current entry
    Set Current_Venue to This_Entry_Venue
    Set Current_Venue_Id to New_Venue_Id
  EndIf
  Echo Appropriate Data (This_Event_Name)
EndForeach

Thank you for your reply StarLion. While I do see where you are coming from here I feel like I’ve implemented your method, but I’m now more lost than I was before as the grid is now all over the place at http://vff.vibethink.net/schedule/. I’ve updated the code below and commented in your instructions to where I think they are relevant can you see where I’m going wrong here?

<?php
					

				
				$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);
				$time = '';//Set Current_Time to Null
				$day_check = '';
				$start_check = '';
				$venue_check = '';
				$gridVenues = ARRAY ( "Culbreth-Theater","Newcomb-Hall","PVCC-Dickinson-Center","Paramount","Regal-1","Regal-2","Regal-3","Regal-4" );
				$currentVenueId = count($gridVenues) - 1;//Set Current_Venue_Id to count(Venue_Array) - 1.
				$totalVenues = count($gridVenues) - 1;

				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');
					$currentVenue = $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'];
											
											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 }?>
						<?php if (in_array($currentVenue, $gridVenues)): ?>
							<?php if ($time != $start_check){//If This_Entry_Time != Current_time
									if($start_check != ''){
										$empties = $totalVenues - $currentVenueId;//Add blank cells equal to count(Venue_Array) - 1 - Current_Venue_Id {$totalVenues = count(Venue_Array) - 1}

										while ($empties > 0) {
											$empties = $empties - 1;
											echo '</div><div class"location-grid cell"></div>';
										}
									}?>

									<div class="grid-row"><!--Start New Row; -->

										<div class="time-grid cell"><!--Add time-display cell (Do Not terminate cell.) -->
											<?php echo $time_format; ?>
										
									<?php
									$start_check = $time; //Set Current_Time to This_Entry_Time 	
									$currentVenue = '';//Set Current_Venue to Null
									$currentVenueId = 0;//Set Current_Venue_Id to 0
							}//Endif ($time != $start_check)?>
							<?php if ($currentVenue != $venue_check){ //If This_Entry_Venue != Current_Venue
									echo "</div>";//Terminate current cell.
									$newVenueId = array_search($currentVenue, array_values($gridVenues));//Find position of new Venue in array.
									$empties = $newVenueId - $currentVenueId ; //Create blank cells equal to New_Venue_Id - Current_Venue_Id
									while ($empties > 0) {
										echo '<div class"location-grid cell"></div>';
										$empties = $empties - 1;
									}
									echo '<div class="location-grid cell event-cell">'; //Start new Cell for current entry
									$currentVenue = $field['choices'][$fieldvalue];//Set Current_Venue to This_Entry_Venue
									$currentVenueId = $newVenueId;//Set Current_Venue_Id to New_Venue_Id
								}?>
											<a href="<?php the_permalink();?>">
												<?php the_title();//Echo Appropriate Data (This_Event_Name)?>

											</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>
														
													</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 endif;?>
							



					<?php	
					
				
				
					$day_check = $day;
					$start_check = $start;
					endwhile; wp_reset_postdata(); remove_filter('posts_orderby','orderbyreplacelocation');?>

Well the page you’re linking to isnt a grid, for starters, it’s a list. Which has a logic pattern that matches my FIRST block of code. (Because you dont care about the positioning of the items horizontally.) Also, the page you’re linking to looks fine to me… can you point to where it’s going ‘wrong’?