Using the relevanssi_hits_filter, it’s easy to separate the search results by post type.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | add_filter('relevanssi_hits_filter', 'separate_result_types'); function separate_result_types($hits) { $types = array(); // Split the post types in array $types if (!empty($hits)) { foreach ($hits[0] as $hit) { if (!is_array($types[$hit->post_type])) $types[$hit->post_type] = array(); array_push($types[$hit->post_type], $hit); } } // Merge back to $hits in the desired order $hits[0] = array_merge($types['mycustomtypethatgoesfirst'], $types['thesecondmostimportanttype'], $types['post'], $types['pages']); return $hits; } |
The basic idea is: first split the search results in separate arrays by post type, then merge then back to $hits[0] in the correct order. The order of search results within the types remains untouched in the process.