Setting the date result of search results to the node creation date in Drupal 6

It took me hours before I finally found the right fix for this. I really don't know what's been the problem at first. I've been using Drupal for years and its only now that I came up with (or noticed) this behavior.

When I do a search in this site, the date Drupal shows on search results page is not the node's creation date. A little googling and I found out that drupal instead shows the date the node was indexed by the search indexer.

But the problem is... I imported a lot of old blog posts in this site from different blog platforms, which means, it was created years ago. So when I do a search, the date that comes out is not the date the actual post was written but when it was indexed.

I came across a blog explaining a fix to this but I can't make it work. I assume that this code is for users of Drupal 5. You can find the code here.

I just made a little modification to the code till finally my search results displayed the correct date the node was created.

Here's what I done for those having similar problems...

In the search module folder that comes with drupal, you'll find the file search.pages.inc.

Open it and search for the function template_preprocess_search_result

you'll find this code just below it

if (!empty($result['date'])) {
    $info['date'] = format_date($result['date'], 'small');
  }

To override the code, copy the function template_preprocess_search_result from search.pages.inc to your template.php located at your theme folder.

function template_preprocess_search_result(&$variables) {
  $result = $variables['result'];
  $variables['url'] = check_url($result['link']);
  $variables['title'] = check_plain($result['title']);

  $info = array();
  if (!empty($result['type'])) {
    $info['type'] = check_plain($result['type']);
  }
  if (!empty($result['user'])) {
    $info['user'] = $result['user'];
  }
  if (!empty($result['date'])) {
    $info['date'] = format_date($result['date'], 'small');
  }
  if (isset($result['extra']) && is_array($result['extra'])) {
    $info = array_merge($info, $result['extra']);
  }
  // Check for existence. User search does not include snippets.
  $variables['snippet'] = isset($result['snippet']) ? $result['snippet'] : '';
  // Provide separated and grouped meta information..
  $variables['info_split'] = $info;
  $variables['info'] = implode(' - ', $info);
  // Provide alternate search result template.
  $variables['template_files'][] = 'search-result-'. $variables['type'];
}

Now, change the word template_preprocess_search_result to yourthemename_preprocess_search_result. ex. If your using zen theme the new name will be zen_preprocess_search_result.

Next, change

$info['date'] = format_date($result['date'], 'small');

to

$info['date'] = format_date($result['node']->created, 'small');

Check if everything is ok and then your done.

Hope that this simple code saves you time ^^

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options