<p>Thanks, the related entries field handle is sidebarTitle.</p>
<p>The first part of the above cleared the errors with an additional .all():</p>
<pre><code>&lt;div&gt;
{% set sidebarChoice = entry.sidebar.with(['sidebar.sidebarTitle']).one() %}
     {% for sidebarEntry in sidebarChoice.sidebarTitle.all() %}
     {% set myEntryQueryUrl = sidebarEntry.url %}
     {% if 'whats-on' in myEntryQueryUrl %}
     {% set myEntryQueryPath = '/whats-on/' %}
     {% elseif 'who-we-are' in myEntryQueryUrl %}
     {% set myEntryQueryPath = '/who-we-are/' %}
     {% else %}
     {% set myEntryQueryPath = '/' %}
     {% endif %}

 &lt;div&gt;
    &lt;h3&gt;{{ sidebarEntry.title }}&lt;/h3&gt;
    &lt;a href=&quot;{{ myEntryQueryPath }}{{ sidebarEntry.slug}}&quot;&gt;
      &lt;p class=&quot;smallcontentblocks-text&quot;&gt;{{ sidebarEntry.title }} text&lt;/p&gt;
    &lt;/a&gt;
  &lt;/div&gt;
{% endfor %}
</code></pre>

CC BY-SA 4.0
2020-09-04T14:46:54.763Z
If you eager load correctly, you won't need the `.all()` in `{% for sidebarEntry in sidebarChoice.sidebarTitle.all() %}` (that's the whole point of eager loading, to prevent extra queries). Double check your eager-loading syntax - it's possible it should just be `.with(['sidebarTitle']`.
James Smith
CC BY-SA 4.0
2020-09-02T15:14:22.393Z
2020-09-02T15:22:31.567Z
2020-09-02T15:22:31.567Z
Jonathan
<p>You don't mention what your related entries field handle is, but remember that calling any related element (entries/assets/categories/users/tags) will require a <code>.all()</code> (or <code>.one()</code> or <code>.ids()</code> or one of the other query-executing methods) unless the related elements are eager-loaded. With that in mind I can't see anywhere in your code where you're executing a call to fetch the related entries from <code>sidebarChoice</code>, and you also seem to have a redundant inner loop. With eager-loading it might look like this:</p>
<pre><code>&lt;div&gt;
  {% set sidebarChoice = entry.sidebar.with(['sidebar.yourRelatedEntriesHandle']).one() %}
  {% for sidebarEntry in sidebarChoice.yourRelatedEntriesHandle %}
      {% set myEntryQueryUrl = sidebarEntry.url %}
      {% if 'whats-on' in myEntryQueryUrl %}
        {% set myEntryQueryPath = '/whats-on/' %}
      {% elseif 'who-we-are' in myEntryQueryUrl %}
        {% set myEntryQueryPath = '/who-we-are/' %}
      {% else %}
        {% set myEntryQueryPath = '/' %}
      {% endif %}

      &lt;div&gt;
        &lt;h3&gt;{{ sidebarEntry.title }}&lt;/h3&gt;
        &lt;a href=&quot;{{ myEntryQueryPath }}{{ sidebarEntry.slug}}&quot;&gt;
          &lt;p class=&quot;smallcontentblocks-text&quot;&gt;{{ sidebarEntry.title }} text&lt;/p&gt;
        &lt;/a&gt;
      &lt;/div&gt;
  {% endfor %}
&lt;/div&gt;
</code></pre>

CC BY-SA 4.0
2020-09-02T15:12:29.093Z
Thanks, the related entries field handle is sidebarTitle.
Jonathan
CC BY-SA 4.0
2020-08-24T18:06:16.490Z
2020-08-24T18:06:16.490Z
James Smith
<p>I'm creating a dynamic side navigation where pages can choose which sidebar it wants to use. This is selected by an entries field called 'sidebar' - called in line 2.</p>
<p>To create this I've made a structure called 'Sidebars' with an entry for each sidebar type.
Each Sidebars entry has an entries field in it which can select any number of pages.</p>
<p>I've got it fully working, but it's giving me deprecation errors.</p>
<p>Here's the working (but deprecated code on line 4):
(lines 5-12 figure out whether the path includes 'whats-on' or 'who-we-are' and adds them to the path to the entry if needed).</p>
<pre><code>&lt;div&gt;
  {% set sidebarChoice = entry.sidebar.one() %}
  {% for rel in sidebarChoice %}
    {% for sidebarEntry in rel %}
      {% set myEntryQueryUrl = sidebarEntry.url %}
      {% if 'whats-on' in myEntryQueryUrl %}
        {% set myEntryQueryPath = '/whats-on/' %}
      {% elseif 'who-we-are' in myEntryQueryUrl %}
        {% set myEntryQueryPath = '/who-we-are/' %}
      {% else %}
        {% set myEntryQueryPath = '/' %}
      {% endif %}

      &lt;div&gt;
        &lt;h3&gt;{{ sidebarEntry.title }}&lt;/h3&gt;
        &lt;a href=&quot;{{ myEntryQueryPath }}{{ sidebarEntry.slug}}&quot;&gt;
        &lt;p class=&quot;smallcontentblocks-text&quot;&gt;{{ sidebarEntry.title }} text&lt;/p&gt;
        &lt;/a&gt;
      &lt;/div&gt;

    {% endfor %}
  {% endfor %}
&lt;/div&gt;
</code></pre>

CC BY-SA 4.0
2020-08-21T01:09:06.403Z
What deprecation error are you seeing?
Marion Newlevant
CC BY-SA 4.0
2020-08-21T10:34:12.457Z
I'm getting this:
Looping through element queries directly has been deprecated. Use the all() function to fetch the query results before looping over them.

And the error is on the line {% for sidebarEntry in rel %}

But when I try {% for sidebarEntry in rel.all() %} I get the Twig Runtime Error page with the notice: Impossible to invoke a method ("all") on a string variable.
Jonathan
<craft3><entries><relations><loop><deprecation>
How to loop through multiple entries fields
# How to loop through multiple entries fields

(Asked by: username_0 on 2020-08-20)

I'm creating a dynamic side navigation where pages can choose which sidebar it wants to use. This is selected by an entries field called 'sidebar' - called in line 2.
To create this I've made a structure called 'Sidebars' with an entry for each sidebar type.
Each Sidebars entry has an entries field in it which can select any number of pages.
I've got it fully working, but it's giving me deprecation errors.
Here's the working (but deprecated code on line 4):
(lines 5-12 figure out whether the path includes 'whats-on' or 'who-we-are' and adds them to the path to the entry if needed).

  {% set sidebarChoice = entry.sidebar.one() %}
  {% for rel in sidebarChoice %}
    {% for sidebarEntry in rel %}
      {% set myEntryQueryUrl = sidebarEntry.url %}
      {% if 'whats-on' in myEntryQueryUrl %}
        {% set myEntryQueryPath = '/whats-on/' %}
      {% elseif 'who-we-are' in myEntryQueryUrl %}
        {% set myEntryQueryPath = '/who-we-are/' %}
      {% else %}
        {% set myEntryQueryPath = '/' %}
      {% endif %}

      
        {{ sidebarEntry.title }}
        
        {{ sidebarEntry.title }} text
        
      

    {% endfor %}
  {% endfor %}

**Tags:** craft3, entries, relations, loop, deprecation

> **Comments:**
> **username_1**: What deprecation error are you seeing?  
> **username_0**: I'm getting this: Looping through element queries directly has been deprecated. Use the all() function to fetch the query results before looping over them.  And the error is on the line {% for sidebarEntry in rel %}  But when I try {% for sidebarEntry in rel.all() %} I get the Twig Runtime Error page with the notice: Impossible to invoke a method ("all") on a string variable.  

---

## 2 answers

### Answer by username_2 on 2020-08-24. Score: 1

You don't mention what your related entries field handle is, but remember that calling any related element (entries/assets/categories/users/tags) will require a .all() (or .one() or .ids() or one of the other query-executing methods) unless the related elements are eager-loaded. With that in mind I can't see anywhere in your code where you're executing a call to fetch the related entries from sidebarChoice, and you also seem to have a redundant inner loop. With eager-loading it might look like this:

  {% set sidebarChoice = entry.sidebar.with(['sidebar.yourRelatedEntriesHandle']).one() %}
  {% for sidebarEntry in sidebarChoice.yourRelatedEntriesHandle %}
      {% set myEntryQueryUrl = sidebarEntry.url %}
      {% if 'whats-on' in myEntryQueryUrl %}
        {% set myEntryQueryPath = '/whats-on/' %}
      {% elseif 'who-we-are' in myEntryQueryUrl %}
        {% set myEntryQueryPath = '/who-we-are/' %}
      {% else %}
        {% set myEntryQueryPath = '/' %}
      {% endif %}

      
        {{ sidebarEntry.title }}
        
          {{ sidebarEntry.title }} text
        
      
  {% endfor %}

> **Comments:**
> **username_0**: Thanks, the related entries field handle is sidebarTitle.  

---

### Answer by username_0 on 2020-09-02. Score: 0

Thanks, the related entries field handle is sidebarTitle.
The first part of the above cleared the errors with an additional .all():

{% set sidebarChoice = entry.sidebar.with(['sidebar.sidebarTitle']).one() %}
     {% for sidebarEntry in sidebarChoice.sidebarTitle.all() %}
     {% set myEntryQueryUrl = sidebarEntry.url %}
     {% if 'whats-on' in myEntryQueryUrl %}
     {% set myEntryQueryPath = '/whats-on/' %}
     {% elseif 'who-we-are' in myEntryQueryUrl %}
     {% set myEntryQueryPath = '/who-we-are/' %}
     {% else %}
     {% set myEntryQueryPath = '/' %}
     {% endif %}

 
    {{ sidebarEntry.title }}
    
      {{ sidebarEntry.title }} text
    
  
{% endfor %}

> **Comments:**
> **username_2**: If you eager load correctly, you won't need the `.all()` in `{% for sidebarEntry in sidebarChoice.sidebarTitle.all() %}` (that's the whole point of eager loading, to prevent extra queries). Double check your eager-loading syntax - it's possible it should just be `.with(['sidebarTitle']`.  

---

