Setting the date result of search results to the node creation date

The date Drupal shows on search results is not the date the node was created. Instead it is the date the node was indexed by the search indexer.
Most of the time this is the wanted behavior. Your nodes get indexed when cron is run and if you run cron each 5 or 15 minutes this date is within 15 minutes of the creation date.

But the date is far off when import your nodes from another system. Sometimes this date has been created years ago, and then the time between creation and indexing is long.

As always there's an easy fix for this in Drupal. The search results are themed via theme_search_item and we all know how to override a theme function.

We just have to change

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

to

  if(isset($item['node'])) {
  	$info[] = format_date($item['node']->created, 'small');
  } elseif ($item['date']) {
    $info[] = format_date($item['date'], 'small');
  }

So we get:

function phptemplate_search_item($item, $type) {
  $output = ' <h3><a href="'. check_url($item['link']) .'">'. check_plain($item['title']) .'</a></h3>';
  $info = array();
  if ($item['type']) {
    $info[] = check_plain($item['type']);
  }
  if ($item['user']) {
    $info[] = $item['user'];
  }
  /*if ($item['date']) {
    $info[] = format_date($item['date'], 'small');
  }*/
  if(isset($item['node'])) {
  	$info[] = format_date($item['node']->created, 'small');
  } elseif ($item['date']) {
    $info[] = format_date($item['date'], 'small');
  }
  if (is_array($item['extra'])) {
    $info = array_merge($info, $item['extra']);
  }
  $output .= ' <div>'. ($item['snippet'] ? '<p>'. $item['snippet'] .'</p>' : '') .'<p class="search-info">'. implode(' - ', $info) .'</p></div>';
  return $output;
}
Written on October 12, 2008 at 09:09, tagged as Drupal, search, things you might have overlooked

Comments

I've done a bit more research, and have managed to find what seems to be the equivalent function in D6, however this code appears not to work:

if(isset($item['node'])) {
$info[] = format_date($item['node']->created, 'small');
} elseif ($item['date']) {
$info[] = format_date($item['date'], 'small');
}

Do you know the d6 equivalent for $item['node']->created ?

thanks

Hi there,

Found this page through google, and this is exactly what I was looking for. I have several thousand nodes to import going back 8 years, and noticed this problem after testing a couple of hundred.

Forgive the noob question, but where does this code go?

thanks

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

About

drupalcoder.com is a blog on all things Drupal in specific and LAMP on OS X in general. It is maintained by Davy Van Den Bremt, a Belgian (Drupal) web developer and designer living in Ghent. The goal of this blog is to log all interesting things that have crossed the writer's path while developing Drupal sites. You can read all about Davy's professional activities on his LinkedIn profile. If you want to get in touch, use the contact form.