I'm using views for my taxonomy term page. I want to sort the nodes in some categories by title, while nodes in other categories by weight. I've been poking around the views UI but I can't figure it out. I was thinking that I probably should implement some hook or create a custom query but I don't know where to start. How do I achieve this?
1 Answer
I accomplished what I wanted to do by using hook_views_pre_build()
and hook_views_query_alter()
/* Remove sorting by weight if viewing a category that should be alphabetized.*/
function commerce_customizations_views_pre_build(&$view) {
$alphabetize = array('3','58');
if($view->name == 'taxonomy_term' && $view->current_display == 'page'
&& in_array($view->args[0], $alphabetize)) {
unset($view->sort['weight']);
}
}
/* Sort by title if viewing a category that should be alphabetized.*/
function commerce_customizations_views_query_alter(&$view, &$query) {
$alphabetize = array('3','58');
if($view->name == 'taxonomy_term' && $view->current_display == 'page'
&& in_array($view->args[0], $alphabetize)) {
$query->add_orderby('node','title','ASC');
}
}