I need to display some specific cck fields such as Telephone Number, Address, etc. at the search result page.

As I see the search-result.tpl.php, there is no $node->... So how can I get the node fields into search result page?

migrated from stackoverflow.com Aug 5 '11 at 13:29

This question came from our site for professional and enthusiast programmers.


In your theme, you can use template_preprocess_search_result()

function YOURTHEME_preprocess_search_result(&$variables) {
  if ($variables['type'] == 'node') {
    $node =& $variables['result']['node'];
    //Do something here

Or, you can also implement hook_nodeapi() in a custom module for $op = 'search result'. The result of hook_nodeapi($node, 'search result') is available in $info_split in the search-result.tpl.php template.


We had to create a custom search once. We created a custom module that allowed not only writing a custom query to the database but also choose the kind of amount that gets displayed. We hooked into hook_search with the module. To give you an idea:

function yourmodule_search($op = 'search', $keys = NULL) {

switch ($op) {
   //some code, basically a stripped version from what the API shows us
    case 'search':

      $find = do_event_search($keys); //the actual search

      // Load results.
      $results = array();

      foreach ($find as $item) { 

        //iteration and adding certain elements to $resuls needned by Drupal
      return $results;

The interessting part for you is the retrival of the search results as done by do_event_search:

//all data comes from a queries to the database
$search_result[] = array ('link' => trim($link),
                    'snippet' => trim_text(i18n_get_lang() == "en" ? $row->description_en : $row->description_de),
                    'head' => trim_text(i18n_get_lang() == "en" ? $row->title_en : $row->title_de),
                    'type' => $type

As you can see, snippet holds the text that should be displayed on the search pages and we select a certain $row from a custom database. The API example shows all fields that are needed for displaying the results properly.



wow, that simple :)


<?php if ($result['node']->field_name[0]['value']): ?>
  <h4><?php print($result['node']->field_name[0]['value']); ?></h4>
<?php endif; ?>

Another possibility is to use Display Suite, it allows you to override the node indexing as well as the display of the search results


you could use the template_preprocess_search_result(&$variables) function (copy it from the search module and place it in your theme's template.php file) - and use the $variables variable (var_dump it) to retrieve your node info and print it

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.