1

I'm trying to create a dropdown filter based off a custom field in channel entries.

{exp:channel:entries channel="events"}

  {!-- Build the complete listing --}
  {exp:stash:append name="listing"}
    <li><a href="{title_permalink='events/detail'}">{title}</a></li>
  {/exp:stash:append}

  {!-- Build the dropdown filter options. This is what I want to contain unique values --}
  {exp:stash:append name="locations"}
    <option>{event_location}</option>
  {/exp:stash:append}

{/exp:channel:entries}

<select>
  {exp:stash:get name="locations"}
</select>

<ul>
  {exp:stash:get name="listing"}
</ul>

This might output something like:

<select>
  <option>USA</option>
  <option>USA</option>
  <option>Australia</option>
  <option>Canada</option>
</select>

<ul>
  <li><a href="...">First US Event</a></li>
  <li><a href="...">Second US Event</a></li>
  <li><a href="...">Australian Event</a></li>
  <li><a href="...">Canadian Event</a></li>
</ul>

However, I want the values in the {exp:stash:get name="locations"} to only be output once.
ie. USA once, not twice.
Is this possible?

Edit:
Turns out it does what I wanted on its own in the code I posted.
However my actual code had a {count} variable thrown in for testing.

{exp:stash:append name="locations"}
  <option>{count} - {event_location}</option>
{/exp:stash:append}

The {count} made each row unique.

1

If you can use the {exp:stash:get_list} tag pair you have access to the unique="yes" parameter. Might try it on the exp:stash:get tag.

2
  • Turns out there was a bit of stupidity on my side. I'll explain it in my question. Although I'd like to know an example of how to properly use the unique="yes" parameter. – Jason Varga Jan 19 '13 at 21:19
  • I've actually never used Stash so can't help with an example. Take a look at the docs that I linked to in my answer. Might help. – Anna_MediaGirl Jan 20 '13 at 2:41
0

You can use a query to do this instead of the channel:entries tag:

{exp:query sql="SELECT DISTINCT field_id_1 as location FROM exp_channel_data WHERE channel_id = '2'"}
    {exp:stash:append name="locations"}
            <option>{location}</option>
    {/exp:stash:append}
{/exp:query}

Change channel_id to match the ID of your events channel and field_id_1 to match your location field ID

3
  • Technically that does work but I want to know if it's possible to do it with Stash. I'll update my example code to explain why. – Jason Varga Jan 19 '13 at 18:22
  • Well you didn't say that! :) – Anna_MediaGirl Jan 19 '13 at 18:26
  • I thought the title of the post implied Stash. I'll update it all in a minute. – Jason Varga Jan 19 '13 at 18:31
0

Stash will automatically deduplicate the values if you are setting the exact same value more than once.

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.