Trouble listing all events
-
I am working on creating an agenda that shows recurring events, very similar to your case. I used the function from one of the other threads to query and sort the events but am having trouble listing the events.
Currently this is in my functions.php file
function my_post_saved($post_id) { if (!$post_id || get_post_type($post_id) !== 'event') { return; } $rrule = get_field('recurring_dates', $post_id); // Update start and end dates in post meta update_post_meta($post_id, 'start_date', $rrule['start_date']); update_post_meta($post_id, 'end_date', $rrule['end_date']); } add_action('acf/save_post', 'my_post_saved'); /** * Get all events between two dates. * * @param mixed $start * @param mixed $end * @return array */ function fbdm_get_events($start = null, $end = null, $data = []) { $events = []; $has_more = false; $index = 0; if (! $start) { // Defaults to today $start = new DateTime(); } elseif (! $start instanceof DateTime) { $start = DateTime::createFromFormat('Y-m-d', $start); } if (! $end) { // Defaults to start date + 1 month $end = clone $start; $end->add(new DateInterval('P1M')); } elseif (! $end instanceof DateTime) { $end = DateTime::createFromFormat('Y-m-d', $end); } $start->setTime(0,0,0); $end->setTime(0,0,0); // Break the loop after 14 events while (sizeof($events) <= 14) { if ($index > 0) { $start = clone $end; $start->add(new DateInterval('P1D')); $end = clone $start; $end->add(new DateInterval('P1M')); } // Query all events which have not ended at start date $args = [ 'post_type' => 'event', 'posts_per_page' => -1, 'post_status' => 'publish', 'meta_query' => [ 'relation' => 'AND', [ 'key' => 'end_date', 'compare' => '>=', 'value' => $start->format('Y-m-d'), 'type' => 'DATE', ], ], ]; $query = new WP_Query($args); // Count posts from start date if (! count($query->get_posts())) { $has_more = false; break; } // Add end date to query arguments $args['meta_query'][] = [ 'key' => 'start_date', 'compare' => '<=', 'value' => $end->format('Y-m-d'), 'type' => 'DATE', ]; $query = new WP_Query($args); foreach ($query->get_posts() as $post) { $post->dates = fbdm_get_event_dates($post->ID); foreach ($post->dates as $date => $array) { $datetime = new DateTime($date); if ($datetime < $start) { continue; } elseif ($end && $datetime > $end) { $has_more = true; break; } // Push the event to the array of dates if (! array_key_exists($date, $events)) { $timestamp = $datetime->getTimestamp() + $datetime->getOffset(); $day = date_i18n("l j F", $timestamp); $events[$date] = [ 'day' => $day, 'month' => date_i18n("F Y", $timestamp), 'events' => [], ]; } foreach ($array as $time) { $time_full = $time['start_time']; if ($time['end_time']) { $time_full .= "-{$time['end_time']}"; } // Update an existing row if (array_key_exists($post->ID, $events[$date]['events'])) { $events[$date]['events'][$post->ID]['time'][] = $time_full; sort($events[$date]['events'][$post->ID]['time']); } // Create a new row else { $events[$date]['events'][$post->ID] = [ 'post' => $post, 'start_time' => $time['start_time'], 'end_time' => $time['end_time'], 'time' => [$time_full], ]; } } // Sort events by start time usort($events[$date]['events'], function($a, $b) { return $a['start_time'] <=> $b['start_time']; }); } } if ($index++ > 3) { break; } } // Sort events array by date (key) ksort($events); return [ 'events' => $events, 'has_more' => $has_more ]; } /** * Get all dates for an event. * * @param int $post_id * @return array */ function fbdm_get_event_dates( $post_id ) { if (! $post_id || get_post_type($post_id) !== 'event') { return; } $dates = []; $recurrence = get_field('recurring_dates'); foreach ($recurrence['dates_collection'] as $date) { if (! array_key_exists($date->format('Y-m-d'), $dates)) { $dates[$date->format('Y-m-d')] = []; } // I created two ACF time fields along with the RRule field // to set the start and end times of each occurrence. // If you don't need to specify the times for your occurrences // you can just push the date to the array instead of using key => value $dates[$date->format('Y-m-d')][] = [ 'start_time' => get_field('event_start_time'), 'end_time' => get_field('event_end_time'), ]; } // Sort dates ksort($dates); return $dates; }
I want to display the event on the custom post type archive page in which I’m calling the function then trying to loop the dates, but I’m getting the wrong dates and too many events at once. Here is what I have:
<?php // Call the function to retrieve events $events_data = fbdm_get_events(); // Limit to 14 posts initially // Check if events are available if (!empty($events_data['events'])) { $events = $events_data['events']; $eventstest = $event['events']; var_dump($eventstest); // Loop through each event date foreach ($events as $date => $event) { // Loop through events for this date foreach ($event['events'] as $event_data) { // Access event details and display them in individual containers $post = $event_data['post']; $start_time = $event_data['start_time']; $end_time = $event_data['end_time']; $event_date = $event['day'] ?? ''; // Assuming 'day' holds the date // Get the permalink for the event $permalink = get_permalink($post->ID); // Wrap the event container with the permalink echo '<div class="event-container cell small-12">'; echo '<a href="' . esc_url($permalink) . '" class="event-link">'; echo '<h3>' . $post->post_title . '</h3>'; echo '<p>Date: ' . $event_date . '</p>'; echo '<p>Start Time: ' . $start_time . '</p>'; echo '<p>End Time: ' . $end_time . '</p>'; echo '</a>'; // Display more event details as needed echo '</div>'; } } // Check if there are more events to load $has_more = $events_data['has_more']; // Display Load More button if there are more events if ($has_more) { echo '<button id="load-more-events">Load More</button>'; } } else { // Handle case where no events are found echo '<p>No events found.</p>'; } ?>
Am I on the right track? I feel like I’m having trouble with the data. Thank you in advanced
Viewing 4 replies - 1 through 4 (of 4 total)
Viewing 4 replies - 1 through 4 (of 4 total)
- The topic ‘Trouble listing all events’ is closed to new replies.