1

I'm looking to show recent categories ordered by their associated entries' postDate.

Is this possible?

  • I don't want to list the entries within the category
  • I simply want to list 4 categories that have recently updated related entries.
  • Entries from any section can be associated to my site's global categories.
1

You could technically do this with a lot of custom SQL, but it would be messy. I think your best bet would be to create a custom Date field on your categories called “Last Used”, and then to write a module that keeps its value updated as entries are saved.

The default Craft 3 project comes with a module file out of the box, so you can use that as a starting point.

First, add this to the top of the module file, after the namespace line:

use Craft;
use craft\elements\Category;
use craft\elements\Entry;
use craft\events\ModelEvent;
use yii\base\Event;

Then in its init() method, add this after the parent::init() line:

Event::on(Entry::class, Entry::EVENT_AFTER_SAVE, function(ModelEvent $event) {
    /** @var Entry $entry */
    $entry = $event->sender;

    if ($entry->enabled) {
        // Loop through the related categories
        foreach ($entry->myCategoriesField as $category) {
            /** @var Category $category */
            // Is this entry's post date more recent than the current Last Used value?
            if ($entry->postDate > $category->lastUsed) {
                $category->setFieldValue('lastUsed', $entry->postDate);
                Craft::$app->elements->saveElement($category, false);
            }
        }
    }
});

You can get Craft to re-save all of your entries by re-saving each of your sections’ settings. (You don’t need to change anything.)

With that in place, you can start sorting by that custom field in your templates.

{% set categories = craft.categories()
    .group('myCategoryGroup')
    .lastUsed('not :empty:')
    .orderBy('lastUsed desc')
    .limit(4)
    .all() %}
  • Thanks Brandon. I'm on a client site that is currently using Craft 2. This will likely make me push to find the time to upgrade it. Unfortunately I have some unsupported plugins in the current install so won't be anytime soon. Many thanks for your time. – Adam Menczykowski Oct 22 '18 at 21:28
  • @AdamMenczykowski Ah ok. You could technically pull something similar off in Craft 2 with a plugin, but there are a couple gotchas, like it’s much harder to update field values programmatically without messing other fields up. – Brandon Kelly Oct 22 '18 at 21:31
  • Thanks, I'll probably work on getting the upgrade to v3 then try your suggestion. – Adam Menczykowski Oct 23 '18 at 13:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.