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(