I want a View with a default sort criteria that can be overridden by a contextual filter. For example, the URL might end like this: ?sort=popular. popular would be associated with one field and recent, the default, would be associated with another field.
This is on Drupal 7 and Views 3.

  • htoip. I changed the term arguments to contextual filters, the new name for arguments in D7.
    – Ashlar
    Feb 13, 2012 at 15:23

3 Answers 3


you can use "exposed filters", they will use url name/value pairs similar to contextual filters. If you don't want to show the exposed filter to the visitor you can hide them.


I am not sure how this translates to Drupal 7 and Views 3, but I have done the following with Drupal 6 and Views 2 using hook_views_query_alter

function mymodule_views_query_alter (&$view, &$query)
    if ($_GET["sort"]) {
        $sort = check_plain(trim(strtolower($_GET["sort"])));

        switch ($sort) {
            case "title":
                $query->orderby = array("node.title ASC");
            case "order":
                $query->orderby = array("node.nid DESC");
            case "somefield":
                $query->orderby = array("node_data_field_somefield_field_somefield_value ASC");

The trick is to make the views w/ the sorts, and look at the SQL, and use this to update your code.

  • Looks like a good answer, but it's a little over my head. I'll have to come back after I've learned some more.
    – htoip
    Feb 13, 2012 at 16:39
  • @htoip Were you able to confirm or deny this?
    – Jeff
    Nov 20, 2013 at 22:17
  • @Jeff I don't think I ever implemented that solution, but I am now familiar with all its parts and it looks like it would work perfectly. Usually that's not how anyone should want to handle arguments, but I had to maintain compatibility with old URL's.
    – htoip
    Nov 21, 2013 at 15:04

In Drupal 7, query will look in following way:

$query->orderby[] = array('field' =>'node_title', 'direction' => 'DESC');

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.