Skip to content

Filtering lists with match against

Mark Croxton edited this page Mar 24, 2014 · 4 revisions

If you want to filter a list by multiple columns, create an extra index column to match against. In this example our list needs to be filtered by channel, by colour, and by one or more categories. Our index takes the form channel/colour/@category_1@category_2@

{exp:stash:set_list name="groceries"}
   
   {stash:id}1{/stash:id}
   {stash:title}Cox Apple{/stash:title}
   {stash:date}5{/stash:date}
   {stash:index}fruits/green/@apples@traditional@{/stash:index}

   {stash:id}2{/stash:id}
   {stash:title}Jazz Apple{/stash:title}
   {stash:date}4{/stash:date}
   {stash:index}fruits/yellow/@apples@modern@crisp@{/stash:index}

   {stash:id}3{/stash:id}
   {stash:title}Banana{/stash:title}
   {stash:date}3{/stash:date}
   {stash:index}fruits/yellow/@bananas@soft@{/stash:index}

   {stash:id}4{/stash:id}
   {stash:title}Brocoli{/stash:title}
   {stash:date}2{/stash:date}
   {stash:index}veggies/green/@plants@{/stash:index}

   {stash:id}5{/stash:id}
   {stash:title}Pepper{/stash:title}
   {stash:date}1{/stash:date}
   {stash:index}veggies/red/@plants@spicy@{/stash:index}

{/exp:stash:set_list}

<h3>5 most recent entries from channel "fruits"</h3>
{exp:stash:get_list name="groceries" match="#^fruits/#" against="index" orderby="date" sort="desc" limit="5"}
 {title}<br>
{/exp:stash:get_list}

<h3>5 most recent entries from channel "veggies"</h3>
{exp:stash:get_list name="groceries" match="#^veggies#" against="index" orderby="date" sort="desc" limit="5"}
 {title}<br>
{/exp:stash:get_list}

<h3>5 most recent entries from channel "fruits" from category "apples"</h3>
{exp:stash:get_list name="groceries" match="#^fruits/.*@apples@#" against="index" orderby="date" sort="desc" limit="5"}
 {title}<br>
{/exp:stash:get_list}

<h3>5 most recent entries from channel "veggies" with color "green"</h3>
{exp:stash:get_list name="groceries" match="#^veggies/green/#" against="index" orderby="date" sort="desc" limit="5"}
 {title}<br>
{/exp:stash:get_list}

<h3>5 most recent entries from channel "fruits" and category "apples" and color "green"</h3>
{exp:stash:get_list name="groceries" match="#^fruits/green/.*@apples@#" against="index" orderby="date" sort="desc" limit="5"}
   {title}<br>
{/exp:stash:get_list}

When accessing a list with {exp:stash:get_list} you can also choose to track which items have already been used in your template, and exclude or include those items in other lists on the same page. Tracking is based on the column name, and works across any lists that share the same column names.

<h2>Bonus: tracking (requires Stash 2.5.1+)</h2>
<h3>5 most recent entries from channel "fruits" and category "apples" and color "green": tracking "id" column:</h3>
{exp:stash:get_list name="groceries" match="#^fruits/green/.*@apples@#" against="index" orderby="date" sort="desc" limit="5" track="id"}
   {title}<br>
{/exp:stash:get_list}

<h3>5 most recent entries excluding items already in the tracked "id" column</h3>
{exp:stash:get_list name="groceries" orderby="date" sort="desc" limit="5" not_in="id"}
   {title}<br>
{/exp:stash:get_list}

<h3>Tracking even works with entirely separate lists, providing the same column name is used</h3>
{exp:stash:get_list name="shopping" orderby="date" sort="desc" limit="5" in="id"}
   {title}<br>
{/exp:stash:get_list} 
Clone this wiki locally