Hey All!
I’m using jet Engine advanced date on a post type called events. I tweaked this Ai generated function so it displays all the recurring dates and post name based on the criteria set. (ie Start Date, Reccuring Weekly for 3 weeks would return :
Event Name 2024.07.19,
Event name - 2024.07.26,
Event name - 2024.08.02.
However I want these results to ve returned as aquery so I can design the results using bricks (And possibly add some other sorting features.
Any ideas?
<script>
// Shortcode to display recurring events
function display_recurring_events_shortcode() {
$events = get_recurring_events();
$output = '';
foreach ($events as $event) {
$output .= $event['post_title'] . ' - ' . $event['event_date'] . '<br>';
}
return $output;
}
add_shortcode('display_recurring_events', 'display_recurring_events_shortcode');
function get_recurring_event_dates() {
global $wpdb;
$query = "
SELECT p.ID, p.post_title, pm.meta_value as recurring_config
FROM {$wpdb->posts} p
INNER JOIN {$wpdb->postmeta} pm ON p.ID = pm.post_id
WHERE p.post_type = 'events'
AND pm.meta_key = 'recurring_dates__config'
";
$results = $wpdb->get_results($query);
if ($results) {
$output = [];
foreach ($results as $result) {
$post_id = $result->ID;
$post_title = $result->post_title;
$recurring_config = $result->recurring_config;
// Decode the JSON config
$config = json_decode($recurring_config, true);
if (json_last_error() !== JSON_ERROR_NONE) {
continue; // Skip this event due to JSON decoding error
}
// Extract recurrence details
$start_date = $config['date'];
$is_recurring = $config['is_recurring'];
$recurrence = $config['recurring'];
$recurring_period = $config['recurring_period'];
$end_type = $config['end'];
$end_after = $config['end_after'];
if ($is_recurring !== "1") {
continue; // Skip this event as it is not recurring
}
// Calculate the recurring dates
$dates = [];
$current_date = new DateTime($start_date);
for ($i = 0; $i < $end_after; $i++) {
$dates[] = $current_date->format('Y-m-d');
// Increment date based on recurrence rule
if ($recurrence === 'daily') {
$current_date->modify("+{$recurring_period} day");
} elseif ($recurrence === 'weekly') {
$current_date->modify("+{$recurring_period} week");
} elseif ($recurrence === 'monthly') {
$current_date->modify("+{$recurring_period} month");
} elseif ($recurrence === 'yearly') {
$current_date->modify("+{$recurring_period} year");
}
}
// Append the post name along with each date to the output
foreach ($dates as $date) {
$output[] = "{$post_title} - {$date}";
}
}
// Return the output as a string
return implode('<br>', $output);
}
return 'No recurring events found.';
}
// Create a shortcode to display the recurring events
function display_recurring_events() {
return get_recurring_event_dates();
}
add_shortcode('recurring_events', 'display_recurring_events');
</script>
Separately, this query returns all the recurring events, but ignores the criteria (ie only returns the first start date)
return [
'post_type' => 'events',
'meta_key' => 'recurring_dates__config',
'meta_query' => array(
array(
'key' => 'recurring_dates__config',
'compare' => 'EXISTS',
),
),
];
In the database
My meta key called
recurring_dates__config
The Value returns:
{"date":"2024-07-14","is_recurring":"1","recurring":"weekly","recurring_period":"1","end":"after","end_after":"10"}
Thanks!