The Drupal Core Search is good, however sometimes we need to enhance It.
The Faceted Search: http://drupal.org/project/faceted_search is a best way!
Faceted Search has many features and one of them is the possibility to attach the results to a View http://drupal.org/project/views
As part a requirement I needed to display links with options to change the default sort order of the Faceted Search Results.
Here you are the solution:
1 - On the Views admin section, add a default sort order to your results.
2 - Add a hook: hook_views_query_alter() to your module, if you don't have a module you should create a custom ones:
<?php
function wilsmodule_views_query_alter(&$view, &$query) {
$sort = $_GET['sort'];
if($view->name=='vw_document_results') {
switch($sort) {
case 1:
$query->orderby[0] = 'node_title ASC';
break;
case 2:
$query->orderby[0] = 'node_data_field_new_business_field_new_business_value DESC';
break;
default:
// will sort by posted date DESC (default)
}
}
}
?>3 - Add the links to pass the query-string parameter to your view:
<div id='sort_results'>Sort results: <a href='?sort=1'>Alphabetically</a> - <a href='?sort=3'>by Posted Date</a> </div>4 - Enjoy!
The query-string parameter will be tested on the Swith clause and then change the Views Sort Order.