For an event page I have the option to filter on cities and countries. Each event has entry fields in which the city and country, of which the values are outputted in a drop down list on the front end.

As some events happen in the same country and city I have multipe values that are the same. Is there any chance to filter these values in Craft and output it only ones?

{% for entry in craft.entries.section('events') %}                      
    <option value="{{entry.stad}}" name="{{ entry.stad }}">{{ entry.stad }}</option>
{% endfor %}

Craft has a group filter, that does just that:

{% for stad, events in craft.entries.section('events') | group('stad') %}
    <option value="{{ stad }}">{{ stad }}</option>
{% endfor %}

As you are outputting the options, build an array of stad values and check for duplicates on each loop iteration. If the value does not exist in the array, add it to the array and print out the option.

{% set stad_array = [] %}

{% for entry in craft.entries.section('events') %}
    {% if entry.stad not in stad_array %}
        {% set stad_array = stad_array|merge([entry.stad]) %}
        <option value="{{entry.stad}}" name="{{ entry.stad }}">{{ entry.stad }}</option>
    {% endif %}
{% endfor %}

Hope this helps!

  • Thanks for the reply :) However, the other option is easier – Evin Wijninga Mar 1 '15 at 11:11
  • Agreed! Glad you found a solution :) – Aaron Berkowitz Mar 1 '15 at 16:04

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.