3

I have 12 content types and a view that shows the title and description of each piece of content.

I want to sort the results of the view as follows: First, node types 1, 4, 5 and 9. Then, node types 2, 6, 10 and 12. And finally, node types 3,7,8 and 11.

Is it possible to sort the results of this way?

Thanks.

  • Do you have any other constraints? e.g. number of results per node type? – zerolab Sep 6 '11 at 23:19
  • no, I don't have any other constraints. – cabita Sep 6 '11 at 23:25
4

you can test this with a custom module to alter the query of the view. for example:

<?php
function nameofmodule_views_query_alter(&$view, &$query) {
    if($view->name == 'nameofview') {
            $query->orderby[0] = "node.type = 'nodetype1'  DESC, node.type = 'nodetype4'  DESC, node.type = 'nodetype5'  DESC, node.type = 'nodetype9'  DESC, node.type = 'nodetype2'  DESC, node.type = 'nodetype6'  DESC, node.type = 'nodetype10'  DESC, node.type = 'nodetype12'  DESC, node.type = 'nodetype3'  DESC, node.type = 'nodetype7'  DESC, node.type = 'nodetype8'  DESC, node.type = 'nodetype11'  DESC";
    }
}
?>
  • It was the first solution and works for me. Thanks. – cabita Sep 7 '11 at 5:51
4

I am unaware of a way to do this using the Views UI.

It should be possible to do this via hook_views_query_alter and using ORDER BY CASE syntax.

Some rough code would look like

function mymodule_views_query_alter (&$view, &$query)
{
  if ($view->name == "the_view_machine_name") {
    $query->orderby = array("
      CASE node.type
        WHEN 'content_type_1' THEN 1
        WHEN 'content_type_4' THEN 2
        WHEN 'content_type_5' THEN 3
        WHEN 'content_type_9' THEN 4
        WHEN 'content_type_2' THEN 5
        WHEN 'content_type_6' THEN 6
        WHEN 'content_type_10' THEN 7
        WHEN 'content_type_12' THEN 8
        ELSE 1000
      END 
    ");
  }
}

If the two content type groups are equivalent, then just make the weights the same for each group.

However, I am not totally sure if I have used this syntax before with this hook before (can't find the site where I used this recently...). Also double check the syntax as this is untested.

4

Tried several things, but the one bulletproof solution that I could come up with is to use Views Attachments

  1. add a page display, filter by node type 1.
  2. add an attachment display, filter by node type 4, attach to the page display, after its content.

Repeat 2 for each of the remaining node types in the desired order.

It will be a big view, but should do the trick.

Edit: that is the best solution I came up with, without an extra module.

1

One idea that might be useful for someone searching for the solution in Drupal 7. I wanted to put one content type first, so this was the solution:

  1. Find the field used to enter taxonomy terms for the vocabulary that you put your content type into
  2. Add that field as the first sorting criteria (desc in my case). It will put that content type in front of the others.

Probably this could work with multiple content types, but in this case one field was enough.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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