From 255d720db64d5e45b7b94a5418efe16b87bde1e4 Mon Sep 17 00:00:00 2001 From: FrankZamora Date: Wed, 3 Dec 2025 11:20:03 -0600 Subject: [PATCH] fix(analytics): Add permalink building to admin-ajax fallback MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When JavaScript falls back to admin-ajax.php instead of the fast search-endpoint.php, results were returned with empty permalinks. This caused click tracking to log URLs in /?p=ID format. Now both endpoints build proper permalinks using post_name: - search-endpoint.php (fast): Already had the fix - admin-ajax fallback: Added permalink building (lines 182-203) Also flushed Redis cache to clear stale results. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- roi-apu-search.php | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/roi-apu-search.php b/roi-apu-search.php index 130dfe5..a34d4a7 100644 --- a/roi-apu-search.php +++ b/roi-apu-search.php @@ -179,6 +179,29 @@ final class ROI_APU_Search_Plugin $offset = ($page - 1) * $per_page; $results = $search->run($term, $per_page, $offset, $category_ids); + // Build permalinks for each result (search engine returns empty permalink) + $site_url = rtrim(home_url(), '/'); + $permalink_structure = get_option('permalink_structure', '/%postname%/'); + + foreach ($results['rows'] as &$row) { + if (empty($row['permalink'])) { + $postName = $row['post_name'] ?? ''; + + if (empty($postName)) { + $row['permalink'] = $site_url . '/?p=' . $row['ID']; + continue; + } + + // Build according to permalink structure + if (strpos($permalink_structure, '%post_id%') !== false) { + $row['permalink'] = $site_url . '/' . $row['ID'] . '/'; + } else { + $row['permalink'] = $site_url . '/' . $postName . '/'; + } + } + } + unset($row); + // Log search for analytics $analytics_cfg = ROI_APU_Search_Analytics::get_config(); $search_id = ROI_APU_Search_Analytics::logSearch(