• Hi !
    I followed this bit of code I found on the knowledge base to sort my search results :

    
    add_filter('relevanssi_hits_filter', 'sort_by_post_type_relevanssi');
    function sort_by_post_type_relevanssi($hits) {
        $types = array();
     
        $types['tribe_events'] = array();
        $types['post'] = array();
        $types['page'] = 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['tribe_events'], $types['post'], $types['page']);
        return $hits;
    }

    It’s working great so far but I have two problems :

    1. I don’t necesserally like the fact that I need to write the post type directly in the code. In relevanssi settings, we can choose which posts type we want. How can we access those ?
    2. How could I display the name of the post type before the post types ? like below :
    EVENTS
        My awesome event
        A great cooperation
        Another event
    NEWS
        A great cooperation
        A super news ! 
    Pages 
        Who are we?
        Contact us
Viewing 1 replies (of 1 total)
  • Plugin Author Mikko Saari

    (@msaari)

    You have to explicitly specify the post types somehow. Relevanssi doesn’t care whether you write them in the code or read the from somewhere else. You can get the list of post types Relevanssi indexes from the option relevanssi_index_post_types with get_option('relevanssi_index_post_types');.

    However, you’ll still have to specify the order of the post types manually, if not in this function, then somewhere else –?Relevanssi will store the post types essentially in random order in the option.

    This user manual page has an example of how to do the headings.

Viewing 1 replies (of 1 total)
  • The topic ‘Group results by post type (and display the post type)’ is closed to new replies.