Is there a way to make a conditional sort criteria with views in D7?

I want to do the following stuff:

if node property = x
  use this sort criteria
  use this one

2 Answers 2


You could achieve this using Views PHP.

Excerpt from the page:

This module allows the admin account (user 1) to add fields, filters and sorts to views which use PHP code. It also supports additional accounts when the core PHP Filter module is enabled.

  • 3
    You could use this module but I think putting PHP into the database is poor form. Commented Sep 3, 2012 at 17:53
 * Implements hook_views_query_alter().
function HOOK_views_query_alter(&$view, &$query) {
  if ($view->name == 'view_name' && $view->current_display == 'page') {
    $query->orderby = array(
        'field' => 'CASE WHEN node.property = x THEN node.y ELSE node.z END',
        'direction' => '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.