Created
March 27, 2017 06:56
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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
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?