I have a view of profile data that I want to sort by lastname. The name field currently has "firstname [middlename] lastname" in it. I looked around and found this example of using views php filter to sort based on a query.

I then made a query which ordered nid's by lastname and plugged that into my view as a filter.


SELECT nid,title,SUBSTRING_INDEX(title,' ',-1) AS lastname FROM node WHERE type='profile' ORDER BY lastname;

Running that query returns the nids in lastname order.
So my whole filter is the following:

$sql="select nid,title,substring_index(title,' ',-1) as lastname from node where type='profile' order by lastname;";
$sql = db_rewrite_sql($sql);
$result = db_query($sql);
while ($row = db_fetch_array($result)) {
$node_ids[] = $row['nid'];
}
return $node_ids;

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.

About the Author...