$slug, 'post_type' => 'page', 'post_status' => ['private', 'publish'], 'posts_per_page' => 1, 'no_found_rows' => true, ]); if (empty($candidates)) { return; } $candidate_id = (int) $candidates[0]->ID; if (!Helpers::acf_ready() || !\function_exists('get_field')) { return; } $rows = \get_field('page_unic', 'option'); if (!is_array($rows)) { return; } foreach ($rows as $row) { if (!is_array($row)) { continue; } if ((int) ($row['page_new'] ?? 0) !== $candidate_id) { continue; } $town_term_id = (int) ($row['town'] ?? 0); $orig_id = (int) ($row['page_old'] ?? 0); if (!$town_term_id || !$orig_id) { continue; } $term = \get_term_by('id', $town_term_id, 'town'); if (!($term instanceof \WP_Term) || $term->slug === '' || $term->slug === $main) { continue; } $orig_permalink = Helpers::without_url_filters( static fn () => \get_permalink($orig_id) ); if (!is_string($orig_permalink) || $orig_permalink === '') { continue; } $path_only = Helpers::remove_domain_link($orig_permalink); $redirect_to = '/' . $term->slug . $path_only; \wp_safe_redirect($redirect_to, 301); exit; } } private static function extract_last_segment(string $uri): ?string { $path = strtok($uri, '?'); if ($path === false) { return null; } $parts = array_values(array_filter( explode('/', $path), static fn (string $p) => $p !== '' )); $last = end($parts); return $last !== false && $last !== '' ? $last : null; } }