Sort Page List by custom date attribute

Permalink 2 users found helpful
I have added a custom date attribute to a number of pages. I would like to use this to sort a page list based on this date from newest to oldest. I've modified page lists controller.php to include a option that uses
$pageList->sortByCollectionAttribute('date');
but it is not working - any ideas on how to do this or what I'm doing wrong?

hursey013
 
Shotster replied on at Permalink Best Answer Reply
Shotster
Just for kicks and grins, try...

$pageList->sortBy('my_attribute_handle', 'desc');

-Steve
hursey013 replied on at Permalink Reply
hursey013
You know what... I think that may have worked, interesting. So are sortByCollectionAttribute and sortByAttribute no longer used or what? Thanks for the help, I appreciate it.
Shotster replied on at Permalink Reply
Shotster
As far as I can tell, sortByCollectionAttribute() is not an implemented method (even though it's invoked in C5's own Calendar add-on). I'm not sure about sortByAttribute.

Anyway, I guess if sortBy() does indeed work, then the other method isn't needed. The default sort order is ascending, so you can omit the second argument in that case.

If you determine that it does indeed work for sure, let us know (and mark the thread as helpful for the benefit of others).

-Steve
hursey013 replied on at Permalink Reply
hursey013
It works, thanks.
alcdev replied on at Permalink Reply
How do you implement this into a custom template for the pagelist?
manuelvalle replied on at Permalink Reply
manuelvalle
A year later, probably I have the answer to your question.

If you want to order PageList output by an attribute you can use the syntax mentioned in your question but changing the words ColletionAttribute with the attribute name you want to sort by.

To order by 'date' attribute use
$pagelist->sortByDate(); 
or 
$pagelist->sortByDate('desc');


To order by "my_special_field" attribute use
$pagelist->sortByMySpecialField();


Remember to form the function name with 'sortBy' and the attribute handle without underlines and with capitalized initials.

Manuel
thobruk replied on at Permalink Reply
That works, so does this in C5.5.1:

$pagelist->sortBy('ak_my_special_attribute');
or
$pagelist->sortByMultiple('ak_my_special_attribute desc', 'cName asc');
foiseworth replied on at Permalink Reply
foiseworth
Great code thobruk!

Just want to highlight to anyone read that you actually do need to put

ak_


before the handle of your custom attribute. In my case, my handle was projectSector, so my code was:

$pl->sortByMultiple('ak_projectSector asc', 'cName asc');
TheRealSean replied on at Permalink Reply
TheRealSean
Does anyone know how to sort by multiple attributes in 5.7?
MrKDilkington replied on at Permalink Reply
MrKDilkington
@TheRealSean

If you find out, let me know.
mesuva replied on at Permalink Reply
mesuva
In 5.7 I've been able to achieve multiple sorting of a PageList by working with the QueryBuilder within it. An example being:
$query = $this->list->getQueryObject();
$query->addOrderBy('ak_my_attribute_handle', 'desc');  // sort by an attribute
$query->addOrderBy('cName', 'asc');  // then sort by something directly on the page record
$query->addOrderBy('ak_another_att_handle', 'asc');

you can also do it this way
$this->list->sortByMyAttributeHandle('asc');  // normal sort function on a PageList object
$query = $this->list->getQueryObject();
$query->addOrderBy('cName', 'asc'); 
$query->addOrderBy('ak_another_att_handle', 'desc');

But you can't only do the $this->list-> way of sorting once, for additional order clauses you have to use the addOrderBy function on the QueryBuilder object.

This is briefly mentioned onhttps://www.concrete5.org/documentation/developers/5.7/working-with-... under the 'Advanced' section
MrKDilkington replied on at Permalink Reply
MrKDilkington
@mesuva

Thank you for the example.

Any chance you want to make a video or tutorial about it?
fh123 replied on at Permalink Reply
fh123
Hello,
i'm a bit confused, i have a custom blog pagelist in bootswatch theme with a custom date from composer. I also want to sort the items on my custom date field.

where can I use the pagelist sort code mentioned above? direct in my blog.php file?

thanks a lot!