1

I have the following Category loop code which I'm trying to sort by the date the entries within have been updated.

e.g if an entry was just updated within a category, this category would be at the top of the page/loop.

Here's my code

  <!-- START CATEGORY LOOP -->
  {% set categories = craft.categories()
      .level('1')
      .limit('8')
      .all() %}

  {% for category in categories %}

       {% set offers = craft.entries({
            relatedTo: category,
            with: ['offerLogo'],
            limit: 10,
       }).all() %}

       {% for offer in offers %}
          {% include '_card' %}
       {% endfor %}

  {% endfor %}

I just can't get them to be ordered. Any help would be great!

1
  • doesn't orderBy: 'dateUpdated desc', work in your offers query? ahh you want to have the categories ordered as well – HendrikEng Jun 27 '19 at 12:49
2

I am not sure if i get what you mean, do you want to have the categories as content blocks, which show the entries ordered by last updated, but the category blocks should be ordered as well e.g. if entry x in categort y is the newest the category block y should be on top of the list?

Otherwise that should work, displaying the latest 10 updated entries on top for 8 categories all in descending order relative the the update date

   {% set categories = craft.categories({
         level: 1,
         limit: 8,
         orderBy: 'dateUpdated desc',
     }) 
  %}

  {% set offers = craft.entries({
     relatedTo: categories,
     with: ['offerLogo'],
     limit: 10,
     orderBy: 'dateUpdated desc',
     }) 
   %}

   {% for offer in offers %}
      {% include '_card' %}
   {% endfor %}

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.