Sort pagelist by numeric custom attribute

Permalink
I'm posting this because it took me forever and a lot of guess to figure this out. Hoping to save others the hassle.

If you are trying to sort by a numeric attribute neither
$pl->sortBy('ak_ATTRIBUTE_HANDLE');
or
$pl->sortByAttributeHandle');

will work as the Attribute seems to be stored as text even if it's a number type.

The only way around this i could find was to use a custom query on the pagelist object. And of course that is poorly documented. So here is my solution

$pl = new \Concrete\Core\Page\PageList();
$pl->getQueryObject()
     ->orderBy('ak_wfov_schedule_hour+0', 'ASC');
$pages = $pl->get();

The key is the '+0' It coerces the sort to be numeric. Using the GetQueryObject allows you to get under the hood of the MySQL query and make direct tweaks not possible through the concrete5 interface.

 
SeanDevoy replied on at Permalink Reply
Thanks for posting. Might come in handy.
Sean