SiteSearch

Adds global search capabilities to October CMS

← Back to SiteSearch Support

How is it possible to sort the results by date?

See this: https://github.com/OFFLINE-GmbH/oc-site-search-plugin/issues/47#issuecomment-336177503

Or this: https://github.com/OFFLINE-GmbH/oc-site-search-plugin/issues/38#issuecomment-302667183

You have to sort the results yourself. You can do this from your page's code section.

An example of sorting ascending/descending based on user input. Hopefully help someone in the future :)

function onEnd() {
    // Get order paramater
    $order = Input::get('order');

    // Set results collection from query
    $resultsCollection = $this->page->components['searchResults']->resultCollection;

    // Set sort Ascending (saleprice)
    $sortAsc = $resultsCollection->sortBy(function($result) {
        return $result->meta['saleprice'];
    });

    // Set sort Descending (saleprice)
    $sortDesc = $resultsCollection->sortByDesc(function($result) {
        return $result->meta['saleprice'];
    });

    // Overwrite the resultsCollection based on order parameter
    switch ($order) {
        case 'price-high-low':
            $this->page->components['searchResults']->resultCollection = $sortDesc;
            break;
        case 'price-low-high':
            $this->page->components['searchResults']->resultCollection = $sortAsc;
            break;
        default:
            $this->page->components['searchResults']->resultCollection = $sortDesc;
            break;
    }    
}

1-3 of 3