get_pdo(); $prefix = $db->get_prefix(); // Build destination URL if empty if (empty($dest)) { // Obtain site_url from database (not HTTP_HOST) $stmt = $pdo->prepare( "SELECT option_value FROM {$prefix}options WHERE option_name = 'home' LIMIT 1" ); $stmt->execute(); $home = $stmt->fetch(PDO::FETCH_ASSOC); $site_url = $home ? rtrim($home['option_value'], '/') : ''; // Try to get post_name for proper permalink $stmt = $pdo->prepare( "SELECT post_name FROM {$prefix}posts WHERE ID = ? LIMIT 1" ); $stmt->execute([$postId]); $post = $stmt->fetch(PDO::FETCH_ASSOC); if ($post && !empty($post['post_name'])) { $dest = $site_url . '/' . $post['post_name'] . '/'; } else { $dest = $site_url . '/?p=' . $postId; } } // Get analytics config $cfg = ROI_APU_Search_Analytics::get_config(); // Log click ROI_APU_Search_Analytics::logClick( $pdo, $prefix, $cfg, $searchId, $postId, max(1, $position), max(1, $page), $dest ); } catch (\Throwable $e) { error_log('ROI APU Search Click Error: ' . $e->getMessage()); // Never break redirect due to tracking // Fallback dest if error occurred before setting it if (empty($dest)) { $dest = '/?p=' . $postId; } } // Redirect to destination header('Location: ' . $dest, true, 302); exit;