Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-09-04T14:46:54.763Z
Answers.Text: 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']`.
Answers.UserDisplayName: James Smith
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-09-02T15:14:22.393Z
Answers.LastActivityDate: 2020-09-02T15:22:31.567Z
Answers.LastEditDate: 2020-09-02T15:22:31.567Z
Answers.UserDisplayName: Jonathan
Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-09-02T15:12:29.093Z
Answers.Text: Thanks, the related entries field handle is sidebarTitle.
Answers.UserDisplayName: Jonathan
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-08-24T18:06:16.490Z
Answers.LastActivityDate: 2020-08-24T18:06:16.490Z
Answers.UserDisplayName: James Smith
Body: <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>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-08-21T01:09:06.403Z
Comments.Text: What deprecation error are you seeing?
Comments.UserDisplayName: Marion Newlevant
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-08-21T10:34:12.457Z
Comments.Text: 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.
Comments.UserDisplayName: Jonathan
Tags: <craft3><entries><relations><loop><deprecation>
Title: How to loop through multiple entries fields
ThreadText: # 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).
  
  ```
  <div>
    {% 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 %}
  
        <div>
          <h3>{{ sidebarEntry.title }}</h3>
          <a href="{{ myEntryQueryPath }}{{ sidebarEntry.slug}}">
          <p class="smallcontentblocks-text">{{ sidebarEntry.title }} text</p>
          </a>
        </div>
  
      {% endfor %}
    {% endfor %}
  </div>
  
  ```
  
  **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:
  
  ```
  <div>
    {% 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 %}
  
        <div>
          <h3>{{ sidebarEntry.title }}</h3>
          <a href="{{ myEntryQueryPath }}{{ sidebarEntry.slug}}">
            <p class="smallcontentblocks-text">{{ sidebarEntry.title }} text</p>
          </a>
        </div>
    {% endfor %}
  </div>
  
  ```
  
  > **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():
  
  ```
  <div>
  {% 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 %}
  
   <div>
      <h3>{{ sidebarEntry.title }}</h3>
      <a href="{{ myEntryQueryPath }}{{ sidebarEntry.slug}}">
        <p class="smallcontentblocks-text">{{ sidebarEntry.title }} text</p>
      </a>
    </div>
  {% 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']`.  
  
  ---
  
  