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 but it is not working - any ideas on how to do this or what I'm doing wrong?
$pageList->sortByCollectionAttribute('date');
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.
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
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
It works, thanks.
How do you implement this into a custom template for the pagelist?
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
To order by "my_special_field" attribute use
Remember to form the function name with 'sortBy' and the attribute handle without underlines and with capitalized initials.
Manuel
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
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');
Great code thobruk!
Just want to highlight to anyone read that you actually do need to put
before the handle of your custom attribute. In my case, my handle was projectSector, so my code was:
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');
Does anyone know how to sort by multiple attributes in 5.7?
@TheRealSean
If you find out, let me know.
If you find out, let me know.
In 5.7 I've been able to achieve multiple sorting of a PageList by working with the QueryBuilder within it. An example being:
you can also do it this way
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
$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
@mesuva
Thank you for the example.
Any chance you want to make a video or tutorial about it?
Thank you for the example.
Any chance you want to make a video or tutorial about it?
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!
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!
-Steve