Adding sorting to a view on Drupal?

coderama picture coderama · Dec 22, 2009 · Viewed 10.3k times · Source

I used to know how to do this, but I can't seem to get sorting to work on a view where filters are exposed in a block. I want to be able to filter by, for example, type, price etc, but then also have sorting options to sort by these items.

How do I get sorting to work like this?

Answer

451F picture 451F · Dec 30, 2009

I used that code to override sorting in non-table views

function views_tweak_views_query_alter(&$view, &$query) {
  if ($view->name == 'products'){
  if (arg(3) == 'pu') $query->orderby[0]='uc_products_sell_price ASC';
  if (arg(3) == 'pd') $query->orderby[0]='uc_products_sell_price DESC';
  if (arg(3) == 'nu') $query->orderby[0]='node_title ASC';
  if (arg(3) == 'nd') $query->orderby[0]='node_title DESC';

  } 
}

and placing into view template links with those urls