Using the Relevanssi (v3.3.5) search plugin on WordPress (v3.9.1) I can group search results by post_type.


add_filter('relevanssi_hits_filter', 'products_first');
function products_first($hits) {
    $types = array();
    $types['page'] = array();
    $types['post'] = array();
    $types['product'] = array();
    // Split the post types in array $types
    if (!empty($hits)) {
        foreach ($hits[0] as $hit) {
            array_push($types[$hit->post_type], $hit);
    // Merge back to $hits in the desired order
    $hits[0] = array_merge($types['product'], $types['post'], $types['page']);
   return $hits;

The bit I can’t get is how to insert a heading between each post type and list unlimited posts.

<!-- list unlimited product results -->
<!-- list unlimited post results -->
<!-- list unlimited page results -->

An example site with post_type grouping on search results page:

Read more here: Group search results by post_type - Relevanssi


If you know the solution of this issue, please leave us a reply in Comment section, to update the question.

Related Wordpress search:

, , , ,

Wordpress related questions and answers: