Custom Post Type Event Displaying Incorrect Year
-
I’ve inherited some code that is displaying my organization’s upcoming events in an array of months. The issue is that the months always display as occurring in the current year regardless of when they will be taking place. I think I need to sort the events by year before sorting and displaying them by month. I’m pasting the code below & here is a gist with additional notes I’ve made to help me figure it out what’s going on.
<?php $eventArray = array(); $currentMonth = date('n'); $currentYear = date('Y'); //Generate our months from the system time. for ($i = $currentMonth; $i <= 12; $i++) { $monthName = date('F', mktime(0, 0, 0, $i, 10)); // March $eventArray[$monthName] = array(); } $returnData = ''; //Custom Post-Type Events $args = array( 'posts_per_page' => -1, 'offset' => 0, 'category' => '', 'orderby' => 'post_date', 'order' => 'ASC', 'include' => '', 'exclude' => '', 'meta_key' => 'FASE_event_date', 'orderby' => 'meta_value', 'post_type' => 'events', 'post_mime_type' => '', 'post_parent' => '', 'post_status' => 'publish', 'suppress_filters' => true ); $allData = get_posts($args); //Search through all our data and sort into months foreach ($allData as $data) { $postDate = strtotime(get_post_meta($data->ID, 'FASE_event_date', true)); //Check if the index exists. If so, then populate that data. if (array_key_exists(date("F", $postDate), $eventArray)) { array_push($eventArray[date("F", $postDate)], $data); } } foreach ($eventArray as $key => $arrayData) { if (count($arrayData) > 0) { ?> <h2><?php echo $key . ' ' . $currentYear; ?></h2> <div class="row"> <?php $reverseArray = array_reverse($arrayData); foreach ($reverseArray as $data) { if (get_post_meta($data->ID, 'FASE_endDate', true) != "") { $startDay = get_post_meta($data->ID, 'FASE_event_date', true); $endDay = get_post_meta($data->ID, 'FASE_endDate', true); $postDate = date("M j, Y", strtotime($startDay)) . " to " . date("M j, Y", strtotime($endDay)); } else { $targetTime = get_post_meta($data->ID, 'FASE_event_date', true) . get_post_meta($data->ID, 'FASE_event_date_start', true); $startTime = date("M j, Y g:s A", strtotime($targetTime)); $postDate = $startTime; if (get_post_meta($data->ID, 'FASE_event_date_end', true) != '') { $targetTime = get_post_meta($data->ID, 'FASE_event_date_end', true); $postDate .= " to " . $targetTime; } } $image = get_the_post_thumbnail($data->ID, 'full', array('class' => 'img-responsive')); if ($image == "") { $image = '<img src="' . get_template_directory_uri() . '/images/fpo_300x200.jpg' . '" class="img-responsive"/>'; } ?> <div class="col-lg-3 col-md-3 col-sm-6 col-xs-6 eventContainer"> <a href='<?php echo get_permalink($data->ID); ?>'> <?php echo $image; ?> <h4><?php echo $data->post_title; ?></h4> <p><?php echo $postDate; ?></p> </a> </div> <?php } ?> </div> <?php } } ?>Additionally, I’d like the events to stop displaying once they’ve passed.
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
The topic ‘Custom Post Type Event Displaying Incorrect Year’ is closed to new replies.