Skip to content

Instantly share code, notes, and snippets.

@croxton
Last active April 8, 2018 01:30
Show Gist options
  • Save croxton/6219973 to your computer and use it in GitHub Desktop.
Save croxton/6219973 to your computer and use it in GitHub Desktop.
Sort a list of entries by year and then by month
	{exp:stash:set_list name="entries" parse_tags="yes"}
		{exp:channel:entries channel="my_channel" orderby="date" sort="desc" dynamic="no" limit="999"}
		    {stash:title}{title}{/stash:title}
		    {stash:date_day}{entry_date format="%d"}{/stash:date_day}
		    {stash:date_month}{entry_date format="%m"}{/stash:date_month}
		    {stash:date_year}{entry_date format="%Y"}{/stash:date_year}
		    {stash:date_sort}{entry_date format="%Y-%F"}{/stash:date_sort}
		    {stash:date_machine}{entry_date}{/stash:date_machine}
		{/exp:channel:entries}
	{/exp:stash:set_list}

	{!-- make a copy containing only the years, so we can find all unique years--}
	{exp:stash:set_list name="years" parse_tags="yes"}
	  {exp:stash:get_list name="entries"}
	      {stash:date_year}{date_year}{/stash:date_year}
	  {/exp:stash:get_list}
	{/exp:stash:set_list}

	{!-- make list of months in reverse order --}
	{exp:stash:set_list name="months"}
		{stash:month}December{/stash:month}
		{stash:month}November{/stash:month}
		{stash:month}October{/stash:month}
		{stash:month}September{/stash:month}
		{stash:month}August{/stash:month}
		{stash:month}July{/stash:month}
		{stash:month}June{/stash:month}
		{stash:month}May{/stash:month}
		{stash:month}April{/stash:month}
		{stash:month}March{/stash:month}
		{stash:month}February{/stash:month}
		{stash:month}January{/stash:month}
	{/exp:stash:set_list}

	{!-- put it all together --}
	{exp:stash:get_list name="years" unique="yes" orderby="date_year" sort="desc"}

	  <h1>{date_year}</h1>

	  {exp:stash:get_list:months name="months"}

	  	{exp:stash:get_list:entries 
	  		name="entries" 
	  		match="#^{date_year}-{month}$#" 
	  		against="date_sort" 
	  		orderby="date_machine" 
	  		sort="desc" 
	  		prefix="entry"
	  	}
	      
	      {if entry:count==1}
	      <h2>{month}</h2>
	      <ul>
	      {/if}

	      <li>{entry:title} ({entry:date_year}-{entry:date_month}-{entry:date_day})</li>

	      {if entry:count==entry:total_results}
	      </ul>
	      {/if}
	    {/exp:stash:get_list:entries}

	  {/exp:stash:get_list:months}

	{/exp:stash:get_list} 

Example output:

	<h1>
		2013 
	</h1>
	<h2>
		May 
	</h2>
	<ul>
		<li> Vitra (2013-05-08) </li> 
		<li> Victoria + Albert Baths (2013-05-08) </li> 
		<li> Vado (2013-05-08) </li> 
		<li> NotOnlyWhite (2013-05-08) </li> 
		<li> Kaldewei (2013-05-08) </li> 
		<li> Bisque (2013-05-01) </li> 
		<li> Hansgrohe (2013-05-01) </li> 
		<li> Majestic Showers (2013-05-01) </li> 
		<li> Inda (2013-05-01) </li> 
		<li> Grohe (2013-05-01) </li> 
		<li> Fantini (2013-05-01) </li> 
	</ul>
	<h2>
		April 
	</h2>
	<ul>
		<li> Kanth (2013-04-12) </li> 
		<li> Bette (2013-04-10)</li>  
	</ul>
	<h2>
		February 
	</h2>
	<ul>
		<li> PomD'or (2013-02-27) </li> 
		<li> Vola (2013-02-27) </li> 
		<li> Lefroy Brooks (2013-02-27) </li> 
		<li> Laufen (2013-02-27) </li> 
		<li> Falper (2013-02-27) </li> 
		<li> Flaminia (2013-02-27) </li> 
		<li> Dornbracht (2013-02-27) </li> 
		<li> Bard &amp; Brazier (2013-02-27) </li> 
		<li> Graff (2013-02-27) </li> 
		<li> Ashton &amp; Bentley (2013-02-27) </li> 
		<li> Agape (2013-02-27) </li> 
	</ul>
	
	<h1>
		2012 
	</h1>
	<h2>
		December 
	</h2>
	<ul>
		<li> Vitra (2012-12-08) </li> 
		<li> Victoria + Albert Baths (2012-12-08) </li> 
		<li> Vado (2012-12-08) </li> 
		<li> NotOnlyWhite (2012-12-08) </li> 
		<li> Kaldewei (2012-12-08) </li> 
		<li> Bisque (2012-12-01) </li> 
		<li> Hansgrohe (2012-12-01) </li> 
		<li> Majestic Showers (2012-12-01) </li> 
		<li> Inda (2012-12-01) </li> 
		<li> Grohe (2012-12-01) </li> 
		<li> Fantini (2012-12-01) </li> 
	</ul>
	<h2>
		April 
	</h2>
	<ul>
		<li> Kanth (2012-04-12) </li> 
		<li> Bette (2012-04-10)</li>  
	</ul>
@stefcab
Copy link

stefcab commented Mar 20, 2017

Thank you a lot for this sorting list example :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment