3

Here is an example of code I'm trying to use to eliminating have to iterate comparing the current entry.title to the related to entry.title then omitting it.

{% set entries = craft.entries.relatedTo(context).limit(MAX_FILLERS).id('not' ~ entry.id) %}

{% set entries = craft.entries.relatedTo(context).limit(MAX_FILLERS)|without(entry.id) %}

Current code I'm trying to get around with a single relatedTo call.

{% set entriesUnfiltered = craft.entries.relatedTo(context).limit(MAX_FILLERS).search('-title:'+entry.title) %}

{% set entries = [] %}

{# Removing self entries #}
{% for e in entriesUnfiltered%}
    {% if e.title not in entry %}
        {% set entries = entries|merge([e]) %}
    {% endif %}
{% endfor %}
Brad Bell
  • 67,440
  • 6
  • 73
  • 143

1 Answers1

2

Your first example (.id('not' ~ entry.id)) will exclude that entry ID from the results on in the actual database query.

Your second example will grab all of the entries that satisfy the criteria, then from the PHP/Twig side, then use the |without filter to loop through the array results and remove the ones that match the criteria.

Pretty sure your 2nd one won't work as you're expecting though, since you're telling it to remove an entry.id, which is an integer, but the results are an array of Entry elements. It might work just using entry instead, but I'm guessing the |without filter isn't smart enough to work on entire objects.

Long story short, use the 1st example.

Brad Bell
  • 67,440
  • 6
  • 73
  • 143