<p>No need for a loop really, just use the <code>filter</code> filter, followed by the <code>keys</code> filter to do the same thing:</p>
<pre class="lang-twig prettyprint-override"><code>{% set entryIds = craft.entries.section('movementEpisodes').orderBy('postDate asc').ids() %}
&lt;h1&gt;Material for the brain #{{ entryIds|filter(item =&gt; item == entry.id)|keys[0] + 1 }}&lt;/h1&gt;
</code></pre>
<p>Less really is more ;-)</p>

CC BY-SA 4.0
2023-03-01T20:23:32.370Z
Thank you! works perfect!
LessMore Creative Studio
CC BY-SA 4.0
2023-03-01T17:04:51.997Z
2023-03-01T17:04:51.997Z
James Smith
<p>I have a section of entries and I would like to output the position of the current entry to the entry template.</p>
<p>I have a code that was working in Craft 3 but it broke in Craft 4 and I am struggling to fix it...</p>
<p>This is my old code:</p>
<pre><code>{% set entryIds = craft.entries.section('movementEpisodes').orderBy('postDate asc').ids() %}
{% set position = '' %}
{% for key, value in entryIds if value == entry.id %}
        {% set position = key + 1 %}
{% endfor %}
&lt;h1&gt;Material for the brain #{{ position }}&lt;/h1&gt;
</code></pre>
<p>I am aware that it is not possible anymore to have an 'if' inside a 'for' loop.
What would be the correct way to do it in Craft 4?</p>
<p>I also tried another approach that didn't work but kept getting the number 1:</p>
<pre><code>{% set entries = craft.entries().section('movementEpisodes').orderBy('postDate').all() %}
{% set currentPosition = 0 %}
{% for entry in entries %}
    {% if entry.id == entry.id %}
        {% set currentPosition = loop.index %}
        {% break %}
    {% endif %}
{% endfor %}
&lt;h1&gt;Material for the brain #{{ currentPosition }}&lt;/h1&gt;
</code></pre>
<p>Any help would be highly appreciated !!</p>

<entries>
Getting the position of the current entry within its section
# Getting the position of the current entry within its section

(Asked by: username_0 on 2023-03-01)

I have a section of entries and I would like to output the position of the current entry to the entry template.
I have a code that was working in Craft 3 but it broke in Craft 4 and I am struggling to fix it...
This is my old code:
{% set entryIds = craft.entries.section('movementEpisodes').orderBy('postDate asc').ids() %}
{% set position = '' %}
{% for key, value in entryIds if value == entry.id %}
        {% set position = key + 1 %}
{% endfor %}
Material for the brain #{{ position }}

I am aware that it is not possible anymore to have an 'if' inside a 'for' loop.
What would be the correct way to do it in Craft 4?
I also tried another approach that didn't work but kept getting the number 1:
{% set entries = craft.entries().section('movementEpisodes').orderBy('postDate').all() %}
{% set currentPosition = 0 %}
{% for entry in entries %}
    {% if entry.id == entry.id %}
        {% set currentPosition = loop.index %}
        {% break %}
    {% endif %}
{% endfor %}
Material for the brain #{{ currentPosition }}

Any help would be highly appreciated !!

**Tags:** entries


---

## 1 answers

### Answer by username_1 on 2023-03-01. Score: 3 | Accepted answer

No need for a loop really, just use the filter filter, followed by the keys filter to do the same thing:
{% set entryIds = craft.entries.section('movementEpisodes').orderBy('postDate asc').ids() %}
Material for the brain #{{ entryIds|filter(item => item == entry.id)|keys[0] + 1 }}

Less really is more ;-)

> **Comments:**
> **username_0**: Thank you! works perfect!  

---

