sortBy('modified') actually sorts by 'created' dates

I’m using the following code to create a list of pages that have recently been added to my site.

<?php $latests = $pages->find('terms')->children()->sortBy('modified', 'desc')->limit(10); ?>

While this code works fine, it seems to me that I should be sorting by “created” date, not “modified” date. You would think that sorting by the “modified” key would return a list of the most recently modified or edited pages, rather than the most recently created pages.

Likewise, the following code returns the page’s creation date, not it’s modified date:

<?php echo $latest->modified('d F Y') ?>

Is this the way Kirby is supposed to return date information related to pages? Or am I missing something?

1 Like

strange. just by looking at the code it should be fine…

$page->modified() returns the magic property calling this function.

which calls toolkit file class.


when updating a $page it calls its touch().

which is just forwarded to plain php touch().

I’m not modifying any files through the Panel, which I am assuming is calling the update() and touch() functions. I’m editing everything locally and FTP’ing to the server. Would that make any difference?

maybe check ftp-client settings to not to preserve modified-date. like in this post but invsers.

Hmmm … I don’t think that would work. If I turn off “Preserve modification dates” then the modification date becomes the date the file was uploaded to the server, which isn‘t always the case. Sometimes I simply move a file to a different directory, which shouldn’t affect the date which the file itself was modified.

it also means that if you upload a file the modification date of the file on the server is kept, right? it will keep the first mod-date forever, right?

so are you sure the modification dates on the server match your expected sorting order?

Please note that Kirby reads the modification date from the page’s directory, not from the content file. The reason is that e.g. if you upload a new image, the page is still modified even though the content file stays the same.

To make the modified value work, you need to touch the directory or tell your FTP program to do so on every change. Kirby can’t magically determine the date of last modification without the help of the file system. :slight_smile:


Hmmm …

This is getting way more complicated than it should be. I think the best way to avoid any modification (or creation) date issues is to include a fixed “created date” field in each new file and used that as the sort key.

Feel free to do so, you can add as many fields as you like. You can also sort by any number of fields. :slight_smile:

Yes, I’ve started implementing this for the last few dozen pages I’ve created. It will work perfectly for my situation.

Thanks for the input.

For those interested, the code I ended up using is this:

<?php $latests = $pages->find('terms')->children()->filterBy('date', '!=', '')->sortBy('date', 'desc')->limit(10); ?>

There’s likely a more efficient way of doing this, but it works!