Change event url
-
I want to change event url ‘https://your-domain/event/20240130_1/‘ but there if the event have Recurring Event that update url like this ‘https://your-domain/event/20240919_1/2024-10-03/‘ so that should be like this ‘https://your-domain/event/20241003_1/‘, so how can I get like this.
here is current code in functions.php// Generate unique slug based on instance date (like 20241003_1)
function custom_set_slug_from_instance_date($post_id) {
if (get_post_type($post_id) !== 'tribe_events') return;
remove_action('save_post', 'custom_set_slug_from_instance_date');
// Get event instance date
$start_date = get_post_meta($post_id, '_EventStartDate', true);
if (!$start_date) return;
$base_slug = date('Ymd', strtotime($start_date));
$count = 1;
$slug = $base_slug . '_' . $count;
while (post_slug_exists($slug, $post_id)) {
$count++;
$slug = $base_slug . '_' . $count;
}
// Update post slug if different
$post = get_post($post_id);
if ($post && $post->post_name !== $slug) {
wp_update_post([
'ID' => $post_id,
'post_name' => $slug,
]);
}
add_action('save_post', 'custom_set_slug_from_instance_date');
}
add_action('save_post', 'custom_set_slug_from_instance_date');
add_action('tribe_events_update_meta', 'custom_set_slug_from_instance_date');
// Check if slug exists (except for this post)
function post_slug_exists($slug, $exclude_id = 0) {
global $wpdb;
return $wpdb->get_var($wpdb->prepare("
SELECT ID FROM $wpdb->posts
WHERE post_name = %s
AND ID != %d
AND post_type = 'tribe_events'
AND post_status != 'trash'
LIMIT 1
", $slug, $exclude_id));
}
// Redirect old-style recurring URLs to proper slug
add_action('template_redirect', function () {
if (!is_singular('tribe_events')) return;
global $post;
$requested_uri = $_SERVER['REQUEST_URI'];
// Match /event/slug/yyyy-mm-dd/ format
if (preg_match('#/event/[^/]+/(\d{4}-\d{2}-\d{2})/#', $requested_uri, $matches)) {
$event_date = $matches[1];
$date = date('Ymd', strtotime($event_date));
$base_slug = $date;
$count = 1;
$slug = $base_slug . '_' . $count;
while (post_slug_exists($slug) && get_permalink(get_page_by_path($slug)) !== get_permalink($post)) {
$count++;
$slug = $base_slug . '_' . $count;
}
$correct_permalink = site_url('/event/' . $slug . '/');
wp_redirect($correct_permalink, 301);
exit;
}
});
// Force correct permalinks for recurring events
add_filter('tribe_events_remove_date_from_url', '__return_true');
add_filter('tribe_events_pro_disable_recurring_event_rewrites', '__return_true');
// Bulk fix slugs (visit /?fix_event_slugs=1)
add_action('init', function () {
if (!current_user_can('manage_options') || !isset($_GET['fix_event_slugs'])) return;
$events = get_posts([
'post_type' => 'tribe_events',
'posts_per_page' => -1,
'post_status' => 'publish',
]);
$fixed = 0;
foreach ($events as $event) {
$start_date = get_post_meta($event->ID, '_EventStartDate', true);
if (!$start_date) continue;
$base_slug = date('Ymd', strtotime($start_date));
$count = 1;
$slug = $base_slug . '_' . $count;
while (post_slug_exists($slug, $event->ID)) {
$count++;
$slug = $base_slug . '_' . $count;
}
if ($event->post_name !== $slug) {
wp_update_post(['ID' => $event->ID, 'post_name' => $slug]);
$fixed++;
}
}
wp_die("✅ Fixed $fixed slugs.");
});
Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
The topic ‘Change event url’ is closed to new replies.