Drupal 7 – Sort the Views PHP value

I use the Views PHP field to retrieve a numeric value from database, but it could not be sorted even after i have checked the Enable click sort and select Sort numerically.

It will throws an error.


It can be fixed by selecting Sort using custom PHP code with the following code.

return $row1->php - $row2->php;


Done =)

Reference: Views PHP sort snippet does not work

11 thoughts on “Drupal 7 – Sort the Views PHP value”

  1. Hi,

    I’ve a view which displays users posts related to specific nodes (suppose comments to nodes), when showing comments I want to show comments of author first and then sort other comments in ascending order. To achieve this I’ve added a PHP field which return integer i.e. if current comment author is same as node author then -1, else comment author.

    I’ve followed the instruction in your post but its not working.

    Have you added any filters to sort with?
    Have you added “value code” in your Global: PHP field


  2. Thanks for the code! Anyone know how to sort within this sort. For example sort numerically with Global PHP, then alphabetically on another field?


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.