Skip to content

Instantly share code, notes, and snippets.

@SagaraZD
Created March 27, 2017 06:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SagaraZD/b259af0343fe169d13c1023cce12b4e9 to your computer and use it in GitHub Desktop.
Save SagaraZD/b259af0343fe169d13c1023cce12b4e9 to your computer and use it in GitHub Desktop.
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();
?>
@alex-kodr
Copy link

This is really helpful thank you. How would you expand upon this by wrapping each post type in its own div and also adding a title?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment