Issue with Automatically Created Events Not Showing on Frontend
-
Hi,
I am encountering an issue with events created programmatically using the
tribe_create_event
function in The Events Calendar plugin. Here are the specifics of the problem:Process:
- A WordPress post is scheduled or published via an automated process (e.g., WP Automatic).
- An event is created programmatically using the
tribe_create_event
function based on the published post. The event includes details such as title, content, start date, end date, categories, organizer, and location. - Although the event is successfully saved in the database and all metadata appears to be correctly set, the event does not display on the frontend immediately.
- To make the event visible on the frontend, I have to manually open the event in the WordPress admin panel and update it.
My code:
add_action(‘save_post’, ‘create_event_on_scheduled_publish’, 10, 3);
function create_event_on_scheduled_publish($post_ID, $post, $update) {
// Checking if the post is already being processed to prevent duplicate eventsif (get_post_meta($post_ID, 'event_created', true) === true) { return;
}
if ($post->post_type == 'post') { //$post->post_status == 'future' &&
// Get the post ID, title, and content
$ID = $post->ID;
$title = get_the_title($ID);
$content = apply_filters('the_content', $post->post_content);
// Extract start and end dates from the content using regex
preg_match_all('/\b\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\b/', $content, $matches);
if (count($matches[0]) >= 1) {
$start_date = $matches[0][0];
$end_date = isset($matches[0][1]) ? $matches[0][1] : '';
// Retrieve post categories by name
$post_categories = wp_get_post_categories($ID);
$event_categories = array();
foreach ($post_categories as $c) {
$cat = get_category($c);
if ($cat) {
// Get the category by name
$event_cat = get_term_by('name', $cat->name, 'tribe_events_cat');
if ($event_cat) {
$event_categories[] = $event_cat->term_id;
}
}
}
// Create event post
// $event = array(
// 'post_title' => $title,
// 'post_content' => $content,
// 'post_status' => 'publish',
// 'post_type' => 'tribe_events',
// 'post_author' => 1, // Ensure a valid author ID is set
// // 'tax_input' => array(
// // // 'tribe_events_cat' => $event_categories,
// // ),
// );
$event_args = array(
'post_title' => $title,
'post_content' => $content,
'post_status' => 'publish',
'EventStartDate' => $start_date,
'EventEndDate' => $end_date,
);
// Insert the event
$event_id = tribe_create_event($event_args);
// Insert the event post into the database
// $event_id = wp_insert_post($event);
// Assign categories to the event
if (!empty($event_categories)) {
wp_set_object_terms($event_id, $event_categories, 'tribe_events_cat');
}
update_post_meta($post_ID, 'event_created', true); // to avoid duplication
// Set event start and end dates
if ($event_id) {
update_post_meta($event_id, '_EventStartDate', $start_date);
if (!empty($end_date)) {
update_post_meta($event_id, '_EventEndDate', $end_date);
}
// Set organizer (whoisin)
$organizer = get_term_by('name', 'whoisin', 'tribe_organizer');
if ($organizer) {
update_post_meta($event_id, '_EventOrganizerID', $organizer->term_id);
}
// Set location (barcelona)
$location = get_term_by('name', 'barcelona', 'tribe_venue');
if ($location) {
update_post_meta($event_id, '_EventVenueID', $location->term_id);
}
// Set map display options
update_post_meta($event_id, '_EventShowMap', '1');
update_post_meta($event_id, '_EventShowMapLink', '1');
// Clear the cache (if a caching plugin is used)
if (function_exists('wp_cache_clear_cache')) {
wp_cache_clear_cache();
}
// Flush rewrite rules
flush_rewrite_rules();
}
} else {
// Log error or handle the case when dates are not found
error_log("Start and/or end date not found in post content for post ID: $ID");
}
}}
Viewing 4 replies - 1 through 4 (of 4 total)
Viewing 4 replies - 1 through 4 (of 4 total)
- You must be logged in to reply to this topic.