Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Organizing search results by post type on WordPress with Relevanssi - A Better Search
//Relevanssi Search results Organizing
add_filter('relevanssi_hits_filter', 'separate_result_types');
function separate_result_types($hits) {
$types = array();
$types['directories'] = array();
$types['post'] = array();
$types['download'] = array();
$types['videos'] = array();
$types['event-exhibition'] = array();
$types['magazines'] = 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['directories'], $types['post'], $types['event-exhibition'], $types['download'], $types['magazines'], $types['videos']);
return $hits;
}
<?php
get_header();
if (have_posts()) :
while (have_posts()) :
the_post();
the_content();
endwhile;
endif;
get_sidebar();
get_footer();
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.