Mecha CMS

Mecha CMS blog and documentation.

Sort Posts by Updated Time

Updated: Sunday, 07 August 2016

How to sort posts by the updated time?

Use the Get::files() method to sort posts based on the updated time:

// Sort article files by updated time
$files = Get::files(ARTICLE, 'txt', 'DESC', 'update');

// Sort page files by updated time
$files = Get::files(PAGE, 'txt', 'DESC', 'update');

// Sort comment files by updated time
$files = Get::files(COMMENT, 'txt', 'DESC', 'update');

Example as a plain widget:

<h4>Last Update</h4>

$files = Get::files(ARTICLE, 'txt', 'DESC', 'update');

for($i = 0, $count = count($files); $i < 7; ++$i) {
    if($i === $count) break;
    $data = Get::articleAnchor($files[$i]['path']);
    echo '<li>';
    echo '<a href="' . $data->url . '">' . $data->title . '</a>';
    echo ' &ndash; <span>' . Date::format($files[$i]['update'], 'Y/m/d H:i:s') . '</span>';
    echo '</li>';


Although, since version 1.0.2 I also have added some update fields into the pages, articles and comments data, but they are not used for this kind of fast sorting purpose.

Since version 1.1.0, you could also use this method to sort posts based on the updated time:

$articles = Get::articlesExtract('DESC', 'update');
$pages = Get::pagesExtract('DESC', 'update');
$comments = Get::commentsExtract('DESC', 'update');
Donation and Email Subscription