I am trying to change my Drupal site's search results. Currently it's showing the default search results by node "Updated or Changed" but i want to change it to node "Created or Published" into Descending ORDER. So all the search results will be ORDER BY CREATED or PUBLISHED. I have looked at many files but couldn't have any success.

Which files do i have to modify and what code?

Any help will be really appreciated.


| improve this question | | | | |

Similar questions here

Use hook_preprocess_search_results and set your criteria there.

Here's the .module file you can use, this sort nodes by their creation time with a descending order. You need to create a .info file to make the module work, also remember to replace [MODULE_NAME] with your own module's name. This is not perfect but at least gives you the data in the order you want.

 * Implement hook_preprocess_search_results()
function [MODULE_NAME]_preprocess_search_results(&$vars) {
        $results = $vars['results'];// via dsm (Devel module), we can see node creation time is available under the 'results' array
        usort($results, function($v1, $v2) {
          return $v2['node']->created - $v1['node']->created; // sort by created time, DESC order
        $value = '';
        $output = '';
        $output_results = array();
        foreach($results as $item) {
          $value .= theme('search_result', array('result' => $item, 'module' => $vars['module']));
          $output_results[] = $value;
          $value = '';
        } // store all results into an array
        $output = implode(" ", $output_results)."<br>"; // convert array to string
        $vars['search_results'] = $output;

.info file:

name = Search Desc
description = sort default search by creation date in DESC order
core = 7.x

search.tpl.php file, you need to copy this file from modules/search/search-result.tpl.php and put it under your theme dir, do not modify the original.

$info_split['date'] = date('m/d/Y - H:i', $result['node']->created);// replace update time with created time
<li class="<?php print $classes; ?>"<?php print $attributes; ?>>
  <?php print render($title_prefix); ?>
  <h3 class="title"<?php print $title_attributes; ?>>
    <a href="<?php print $url; ?>"><?php print $title; ?></a>
  <?php print render($title_suffix); ?>
  <div class="search-snippet-info">
    <?php if ($snippet): ?>
      <p class="search-snippet"<?php print $content_attributes; ?>><?php print $snippet; ?></p>
    <?php endif; ?>
    <?php if ($info_split): ?>
      <p class="search-info"><?php print $info_split['user'].' '.$info_split['date']; ?></p>
    <?php endif; ?>

Update: Given Drupal's default search ranking criteria (by content relevance), an additional hook is needed inside the .module file for OP's question. After adding this, you need to go to /admin/config/search/settings and set the Influence for the 'Node Creation Date' field at the bottom of the page, re-index, and run cron.

 * Implement hook_ranking()
function [MODULE_NAME]_ranking() {
  return array(
    'node_created_date' => array(
      'title' => t('Node Creation Date'),
      'score' => 'n.created',
| improve this answer | | | | |
  • Thanks for your response. So how can i change my search.api.php etc file to take effects on search results to display created and order by desc sort, can you please post the code for that if you already have so i can put in my file and it runs? It's Drupal 7. – Naqeeb Feb 23 '17 at 20:46
  • You should not edit any files inside Drupal core. Instead let's go with a custom module and run our hook function there. I'll update the answer when I get a chance to write the code...Thanks. – cchen Feb 23 '17 at 20:49
  • Hey! Thanks for the code. I'm sorry i'm asking so many questions. So i have created the searchdesc.info & searchdesc.module files in "site/modules/search", pasted your code in searchdesc.module file and changed the function name to "searchdesc_preprocess_search_results" but what should be in the searchdesc.info file? – Naqeeb Feb 27 '17 at 17:44
  • Okay so for custom modules, you need to save them inside docroot/sites/all/modues/custom/searchdesc/ Could you try this and see if it's working? Don't forget to enable it :) Edited answer with .info file content too. – cchen Feb 27 '17 at 17:47
  • Nope :(, it did not work. The module is enabled now, in the docroot/sites/all/modues/custom/searchdesc/ path. – Naqeeb Feb 27 '17 at 18:09

As an alternative to the hook_preprocess_search_results solution you could also use the search_api in combination with the views module as it will allow you to customize your search results much easier.

  • Download search_api search_api_db entity views ctools
  • Enable Search API, Search views, Database search, Entity API, Views,Views UI,Ctools
  • Go to admin/config/search/search_api/add_server fill in some server name and choose Database service as Service class
  • Go to admin/config/search/search_api/index/default_node_index and press Queue all items for reindexing and then Index now
  • Enable highlighting at admin/config/search/search_api/index/default_node_index/workflow
  • Reindex again
  • Create a page view of Default Node Index - Create a page - display format: unformatted list of Rendered Entity
  • Add a sort filter of Indexed Node: Date created (desc)


| improve this answer | | | | |
  • Isn't there any way to modify Drupal Search code to show the search results in ORDER BY created not updated? – Naqeeb Feb 27 '17 at 14:01
  • Yes but you have to use @cchen option. This is just an alternative way of doing it. – undersound Feb 27 '17 at 14:37
  • I need direction for that, which file and what code inside, so i can paste in there and it works. Do you have any example? Thanks! – Naqeeb Feb 27 '17 at 17:15

I have achieved sorting by creation date by using the hook_query_alter function in a custom module. Following blog post let me in the correct direction.

 * Implements hook_query_alter().
 * @param QueryAlterableInterface $query
function MY_CUSTOM_MODULE_query_alter(QueryAlterableInterface $query) {
 // Alter the core node search query to sort by creation date.
 $is_search = $is_node_search = FALSE;
 $node_alias = FALSE;
 foreach ($query->getTables() as $table) {
   if ($table['table'] == 'search_index') {
     $is_search = TRUE;
   if ($table['table'] == 'node') {
     $node_alias = $table['alias'];
     $is_node_search = TRUE;

 if ($is_search && $is_node_search) {
   $query->orderBy('created', 'DESC');
| improve this answer | | | | |

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.