I'm trying to use this slightly differently: How do I omit similar content in a for loop based on the first letter in title?

I have a for loop with several entries. One field is a drop-down with a bunch of vendors listed, and the user can select only one. I want a list of vendors that have been selected in the entries so that somebody can click on the vendor and see only the entries associated with that vendors. Clear as mud?

If no vendor is in any entries, then it should not be on the list.

{% set vendorArray = [] %}

{% for entry in craft.entries.section('orderBook').order('vendor').all() %}
  {% if entry.vendor not in vendorArray %}
    {% set vendorArray = vendorArray|merge([entry.vendor]) %}
  {% endif %}
{% endfor %}

{% for link in vendorArray %}
  <a href="{{ siteUrl }}{{ link }}">{{ link }}</a>
{% endfor %}

The above link shows this working with only using the first letter of a field. I want to use the entire field. I removed the filter |first and then I start to get duplicate vendors. I think the solution has something to do with the operator "not in", but I'm a noob so not too sure.


Currently you are trying to compare objects with each other and of course they are always different since they have a different reference. entry.vendor is not a single selected value but the field with all it's options and possible values.

Please try

{% for entry in craft.entries.section('orderBook').order('vendor').all() %}
    {% if entry.vendor.value not in vendorArray %}
        {% set vendorArray = vendorArray|merge([entry.vendor.value]) %}
    {% endif %}
{% endfor %}

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.