I wan't to order my search result first by publish date (or my custom date field), and then by relevance. Recently posted on Content ranking are not working. How to do this?


I assume you are using the core Search module. For this kind of functionality I prefer using the excellent Search API module. There are other modules as well such as Solr but with increased complexity.

The core search module indexes only node title and body. Using the module not only allows you to index other fields but also to present the results in any fashion by creating a custom view with the Views module.

In order to use Search API, you need a backend such as the Search API DB module. This needs to be installed and activated along with Search API as well.

In order to create a custom page of results:

  • Create a search server in Search API configuration
  • Create an index with all relevant fields THAT you are going to use in the view (next) as well
  • Create a new view of type index
  • Add an exposed filter 'Fulltext Search' if you wish
  • Add any fields you wish to present, usually the rendered entity field (teaser)
  • Sort by any field you prefer to.

Did you try the solution from this answer?

    function mymodule_preprocess_search_results(&$variables) {
        $variables['search_results'] = '';
        $output_results = '';
        $output = '';

        // loop through results, group by department
        $departments = array();
        foreach ($variables['results'] as $result) {
                $departments[$result['node']->field_department[$result['node']->language][0]['value']][] = $result;

        foreach ($departments as $department => $departmentResults) {
            $value = "";
            foreach ($departmentResults as $key => $result) {
                $value .= theme('search_result', array('result' => $result, 'module' => $variables['module']));

            $output_results .=  '<div id="tabs"><h3>' . $department . '</h3>';
            $output_results .=  $value;
            $output_results .=  '</div>';

        $output .= '</ul>';
        $output .= $output_results;
        $output .= '</div>';

        $variables['search_results'] = $output;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.