0

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?

2
  • why not just create different displays in views for each content type with the custom sort criteria for each?
    – schnippy
    Commented May 7, 2013 at 20:26
  • it's not content types; it's term ids/term pages.
    – Pax
    Commented May 7, 2013 at 20:35

1 Answer 1

0

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');
   }
 }  

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.