ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Get a list of all years containing entries

March 06, 2012 3:46pm

Subscribe [2]
  • #1 / Mar 06, 2012 3:46pm

    skuba

    6 posts

    I’m trying to find a way to list all years where there is an entry in a particular channel. I’ve considered using a category for this, but it seems redundant as this is an events channel and there is already a date associated with each entry.

    The reason for this is that I have a jQuery filter I wrote that dynamically loads entries based on year. The way I plan on approaching this (if I can’t find a better answer) is looping over all entries in the channel, pushing all date years into an array, compare the years to limit to one instance per year, and then loop over the array to make a UL of the entries resulting in something like:

    <ul>
      <li>2012</li>
      <li>2011</li>
    </ul>

    However, this seems like an incredible amount of requests (say if there are 500 events) to find out what years have an entry inside them. Just wondering if there is a better way in the EE syntax that I didn’t come across in my extensive searching.

    Thanks in advance!

  • #2 / Mar 07, 2012 2:46pm

    Dan Decker

    7338 posts

    Hi skuba,

    Welcome to the Forums, thanks for posting your question!

    This is easy enough to do using the Month Archive tags in the Channel Module.

    {exp:channel:month_links channel="news" limit="50"}
     {year}
    
    {/exp:channel:month_links}

    Let me know if you have any questions!

     

     

  • #3 / Mar 08, 2012 3:08pm

    skuba

    6 posts

    This is exactly what I needed! The more I use EE the more impressed I am with the thought behind the whole framework. Much appreciated Dan!

    For anyone else following this there is also a free add-on I came across called Low Yearly Archives which has some additional functionality, but the best solution for my issue is Dan’s suggestion. Why not use the native code if it’s all you need?

    Thanks again!

  • #4 / Mar 09, 2012 12:12pm

    skuba

    6 posts

    Okay so this is all good, but I just realized that it’s not quite a perfect solution. I can modify my back-end to fit this, but it means dropping the standard Entry Date (or “created date” as I see it) to be used instead as the Event Date. Is it possible to use the Archive Month Tag in relation to a specific Date Channel Field or is it limited to Entry Date alone?

    Thanks,
    Jay

  • #5 / Mar 09, 2012 4:04pm

    Robin Sowell

    13255 posts

    Hrm… well, it would be simple to do a custom plugin based on the month_link function.  But for something really quick, you could use the query module:

    {exp:query sql="SELECT DISTINCT year(FROM_UNIXTIME(entry_date)) AS year FROM exp_channel_titles WHERE channel_id = x"}
    {year}
    
    {/exp:query}

    This is basically the query used by the month link function.  Change entry_date to be field_id_x- where ‘x’ is the correct field_id for the field holding your date.  Change channel_id = x so that x is the id of the channel you want pulled back.

    Now- the only thing this really lacks is localization- which is probably not critical when it comes to year links.  (Meaning the query is calculating year based on gmt date.  You can see how the code compensates for this- and if you need to do that, it’s probably time to write a plugin.  You probably don’t need to do that, though.)

    Anyway- does this make sense?  And does it seem to get you what you need?

    And depending on what all else you need to do on the site- having the event date be the entry date may still be a better way to go.  For this particular issue- it is something you should be able to work around pretty easily.  If there are other issues- it may be worth using the entry_date field.

  • #6 / Mar 09, 2012 4:46pm

    skuba

    6 posts

    Thanks for the reply Robin! I actually decided to do the work around as suggested by Dan and use the Entry Date as the Event Date, but I found a small bug that’s driving me nuts. As I was doing testing before launch I noticed that if I edit the Entry Date of an existing entry (as the client may do if they wanted to change the date of an event), I found that it causes the month_links tag to duplicate the year.

    It only seems to happen if the date is moved backwards from the original date, which probably won’t happen often… but since it causes the loop (and my site) to break I thought I’d bring it up. I posted a separate entry about the issue here: http://ellislab.com/forums/viewthread/212852/

    It seems strange that it would happen since as you said the SQL Select uses Distinct, so I’m not sure why this is happening. Any advice would be great. Thanks!

  • #7 / Mar 09, 2012 6:02pm

    skuba

    6 posts

    Just a heads up: I used the query module and no problems. Not sure why this was happening in the month_links tag.

    Thanks!

  • #8 / Mar 11, 2012 12:43am

    Dan Decker

    7338 posts

    Hi skuba,

    I’m glad to see the Query module got you the results you are looking for!

    If you need anything later on, just let us know in a new thread!

    Cheers,

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases