3

I seem to be having trouble with the performance of my site (on localhost) after including this object. Im using it to build a menu for an archive of the posts section which has about 2,500 entries inside of it.

{% set allEntries = craft.entries.section('posts').limit(null) %}

{% for year, entriesInYear in allEntries | group("postDate.year") %}
    {{ year }}
{% endfor %}

This fetch request spits out the following:

2016 2015 2014 2013 2012 2011 2010 2009

When I make this fetch request, my page is reload time is ~11s. If I comment out this request my page reload time is ~3s.

Is there a more economical way to return the years associated with my posts?

5

If you know which years should appear, and there are no breaks in the timeline, perhaps it would make more sense to do a simple numeric loop instead...

{% for year in (now.year)..2009 %}
    {{ year }}
{% endfor %}

That will count backwards from the current year to 2009.

| improve this answer | |
  • 3
    Or you could use the oldest entry's postDate with {% set oldestEntry = craft.entries.section('posts').order( 'postDate asc' ).first() %} then use oldestEntry.postDate|date( 'Y' ) is place of 2009. – Luke Pearce Jul 4 '16 at 5:58
  • NICE idea @Luke Pearce! – Matt P Jul 4 '16 at 8:47
  • Great point @LukePearce! You could do the same to get the newest entry (in case it's not from the current year). So at most, you'd only be querying for 2 entries. – Lindsey D Jul 4 '16 at 14:53
  • Perfect, thank you Lindsey and @LukePearce ! – Bryant Wells Jul 4 '16 at 16:30

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.