Drupal: Display Creation Date in Search Results

I've been pretty happy with this site since I migrated to Drupal a year ago.  Out of the box it's not as full-featured with respect to blogging as dedicated blogging tools like WordPress or MovableType, but that's not really an issue for me.  Plus, I've added bits and pieces to the site which have made it easier for me to use (post articles) and manage.

Still, there's been one issue that's been bothering me since the beginning and that has to do with how search results are displayed.

By default, search results are presented with each article's "last modified" date displayed.  Usually this is the proper behavior, but since I imported the site from WordPress last year all imported articles had a modified date of August 2009.  I did not like that, but my initial investigation in how to display the created date instead of the modified date yielded no results.

Fast forward to today when I decided to take another look.  Lo and behold, what did I find?  An excellent article that provided exactly the solution I needed, along with the instructions on how to implement it the right way, the Drupal way.  Thanks to Aaron over at the RoamingCoder for this wonderful fix!

Here's the link to Aaron's article on Displaying Creation Date in Drupal Search Results.

For my own reference, here are the details.  First, copy search-results.tpl.php and search-result.tpl.php from $drupalroot/modules/search to your theme's folder.  Then add the following code to search-result.tpl.php, right after the opening comments. If you don't see the created date in your search results, visit the theme admin page and resave your theme.

 if($type == "node"){
   $info_split['date'] = format_date($result['node']->created, 'small');
   $info = implode(' - ', $info_split);
 }

 

The featured image (which may only be displayed on the index pages, depending on your settings) was randomly selected. It is an unlikely coincidence if it is related to the post.

1 thought on “Drupal: Display Creation Date in Search Results”

  1. Thanks for this helpful information. In my case it only works after removing the surrounding

    if($type == “node”){ }

    I’m using Drupal 7.41.

Comments are closed.