Drupal 7 Skip to First Search Result if Only One

If you want to alter the default Drupal 7 search so that it skips to the URL of the first result if there is only one then add the following code to your template.php

Change <template> with your template name.

 

 

 

 

function <template>_preprocess_search_results(&$variables) {

$resultcount =0;
foreach ($variables[‘results’] as $result) {
$resultcount += 1;
}

if($resultcount == 1){
drupal_goto($variables[‘results’][0][‘link’]);
}
}

 

Leave a Reply