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

Example:

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.

<h1>Products</h1>
<!-- list unlimited product results -->
<h1>Posts</h1>
<!-- list unlimited post results -->
<h1>Pages</h1>
<!-- list unlimited page results -->

An example site with post_type grouping on search results page: http://scotreferendum.com/?s=BBC

Read more here: Group search results by post_type - Relevanssi


Solution:

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: