1

I've got a website with lots of content that has been migrated. For each node I've added the original date as the Authored on date. However, search snippets show the date a node was last updated.

I've read the search-result.tpl.php file and can't figure out how to change the $info_split['date'] variable so that it shows the publication date instead.

How can I display the Authored on date instead of the updated date in the search results?

3

Use a the preprocess function, template_preprocess_search_result() to add a custom authored on date variable to your search-result.tpl.php template.

function MYTHEME_preprocess_search_result(&$variables) {
  //$variables['result']['node'] is the node object returned in the search results
  //the $variables['result']['node']->created property contains the authored on date
  if(isset($variables['result']['node'])) {
    $variables['created_date'] = format_date($variables['result']['node']->created, 'short');
  }
  //$created_date will be available in your search-result.tpl.php for printing
}

It's probably worth mentioning that the base template is printing out the $info variable which is the concatenation of a few vars in $info_split. Changing $info_split won't change the printed date without changing search-result.tpl.php.

  • Doesn't seem to the trick, unless I'm doing something wrong. When I use <p>Published on <?php print $created_date; ?></p> or <p>Published on <?php print date("jS F Y", $created_date); ?> in search-result.tpl.php I don't get a date (just the text "Published on"). – rkhff Oct 5 '15 at 22:17
  • Pass by ref typo, sorry. Add the & operator to $variables. – Shawn Conn Oct 6 '15 at 2:43
  • Ah, I should have spotted that myself! It's working now - many thanks! – rkhff Oct 6 '15 at 9:03

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.