Usually, it works fine when I rearrange two or more sorting criteria within a Views (simple drag and drop) but in my case (Content fields grouped by taxonomy terms), one of the sorting criteria is a Global:PHP (based on a PHP field) and it seems like it is always treated first whatever level I choose? And of course, I need it to be treated secondly.

EDIT1 : Here is the problem in details

I have a list of nodes with pagination (1-15, 16-28, etc.). I also have a taxonomy field to group the nodes (categories). I need to sort the nodes by taxonomy terms (according to the weight) and secondly by first page. I get the first page using a PHP field to extract the data from the pagination. If I use the pagination field directly, the sorting is wrong (page 100 will appear before page 11, "alphabetical order").

Finally, I need :

Term 1

Node 1 pages 1-15

Node 4 pages 78-100

Node 12 pages 167-184

Term 2

Node 2 pages 16-45

Node 10 pages 110-125

Term 3


Any idea?


Related posts :

Cant get desired results for multiple sort criteria in a view

Sort criteria is ignored in Views

Views: custom field for sort criteria

EDIT2 : As suggested by Smalution, a custom module could help. I am thus working on the little code needed to sort my View using a custom PHP field. I am using hook_views_query_alter and add_orderby but I can not find the right way to write it since the custom PHP field is not stored in a table like any other standard Drupal fields (the PHP field is computed when the View is calculated). Could anyone help me to complete the code given by Smalution? Thanks for any tip. Tell me if more details are needed.

1 Answer 1


You can go for hook_views_query_alter.

Alter the query before executing the query.

function module_name_views_query_alter(&$view, &$query) {
  if ($view->name == 'Viewname') {
   // put your modified sorting criteria
  • I have tried to create a custom module as you suggested but in my case, I need a value computed in a PHP field. If I am correct (newbie in module programmation), if I use $query->add_orderby, for example, I must define a table, a field and the sort direction. With a PHP field, I do not have a table and a field (only $row->php?). How could I write this little code? Thanks in advance.
    – Toki
    Sep 30, 2016 at 13:28

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.