<p>I'd suggest breaking your problem down into code which gathers the IDs of each of the types of entries first, then outputs them....roughly like this (very much untested!! - but should be enough to understand the approach):</p>

<pre><code>{# Arrays to store the IDs of the entries as we find them #}
{% set noDateIDs = [] %}
{% set dateIDs = [] %}

{% for entry in craft.entries.section('heroes').order('date') %}

    {% if entry.date|length %}
        {% set dateIDs = dateIDs|merge([entry.id])} %}
        {# Or of course you could output the data you want here directly but I like to keep my building and output separate for clarity #}
    {% else %}
        {% set noDateIDs = noDateIDs|merge([entry.id])}  %}
    {% endif %}

{% endfor %}

{% set noDateIDsString = noDateIDs|join(', ') %}
{% set dateIDsString = dateIDs|join(', ')  %}

{# Now actually output the dated entries #}
{% for entry in craft.entries.section('heroes').id(noDateIDsString).find() %}

    &lt;article&gt;
        &lt;h3&gt;&lt;a href="{{ entry.url }}"&gt;{{ entry.title }}&lt;/a&gt; - {{ entry.date|date("j") }}{{ entry.date|date("S") }} {{ entry.date|date("F") }} {{ entry.date|date("Y")}} - {% for category in entry.genre %}{{ category.title }}{% endfor %}&lt;/h3&gt;
    &lt;/article&gt;

{% endfor %}

{# Output the undated ones ordered by postDate #}
{% for entry in craft.entries.section('heroes').id(dateIDsString).order('postDate') %}

        &lt;article&gt;
            &lt;h3&gt;&lt;a href="{{ entry.url }}"&gt;{{ entry.title }}&lt;/a&gt;&lt;/h3&gt;
        &lt;/article&gt;

{% endfor %}
</code></pre>

<p>This approach is very useful generally in Craft, and is documented really well <a href="http://webstoemp.com/blog/manipulating-craft-elementcriteriamodel-with-twig/?utm_campaign=Craft%2BLink%2BList&amp;utm_medium=email&amp;utm_source=Craft_Link_List_10" rel="nofollow">here</a> in 'Manipulating Craft's ElementCriteriaModel objects with Twig' by Jérôme Coupé of Webstoemp </p>

CC BY-SA 3.0
2016-02-28T05:04:12.767Z
You *can* probably pass  the ids directly as an array rather than doing that string conversion, not sure on that...
Jeremy Daalder
CC BY-SA 3.0
2016-02-28T07:00:37.283Z
Apologies, I rephrased my question. Your answer is great for weeding out the ones without dates but I still need to loop through the categories, first and foremost.
MikeeBee
CC BY-SA 3.0
2016-02-29T01:27:26.340Z
Just to finish off our slack convo it's easy to loop over categories.  Within you could e.g. loop dates and get the 1st entry for that cat on that date. I still don't think that's quite what you want - the better approach would be list all the events that are under a category by date, or  list events on a date by ctaegory.  Looping both categories and dates is tricky & I am not even convinced the output would make much sense from a presentation point of view so I'd advise talking to the client more and showing them more real world examples of where this might not make much sense.  Just my 2c!
Jeremy Daalder
CC BY-SA 3.0
2016-02-29T06:05:16.763Z
Agreed. The output will make no sense and be unhelpful to the user. I'm pushing back to the client in the hope that they see sense! Still might have to do it though :/
MikeeBee
CC BY-SA 3.0
2016-02-28T04:52:51.203Z
2016-02-28T05:00:24.923Z
2016-02-28T05:00:24.923Z
Jeremy Daalder
<p>I have a channel with entries that may or may not have a custom date field populated.</p>

<p>I'd like the display a list of entries, looping through each category, showing one entry from each category. Then just start the loop again.</p>

<p>Then, show the rest of the entries which <strong>don't</strong> have the date field populated. I guess they would just have to be just ordered by postDate, but still only showing one per category each loop.</p>

<p>So (assuming there are 3 categories in total)...</p>

<ol>
<li>Entry - 4th April - Category 1</li>
<li>Entry - 6th April - Category 2</li>
<li>Entry - 8th April - Category 3</li>
<li>Entry - 5th April - Category 1 - <em>start the loop through categories again</em></li>
<li>Entry - 7th April - Category 2</li>
<li>Entry - 9th April - Category 3</li>
<li>Entry - Category 1</li>
<li>Entry - Category 2</li>
<li>Entry - Category 3</li>
</ol>

<p><strong>Note that the dates appear jumbled</strong>, as the categories are the thing dictating the order. Date order is a factor, but secondary to the looping of the categories.</p>

<p>This is the code I have so far, all it does is output the entries but with the ones without date fields populated appearing first.</p>

<pre><code>{% for entry in craft.entries.section('heroes').order('date') %}

    {% if entry.date|length %}

        &lt;article&gt;
            &lt;h3&gt;&lt;a href="{{ entry.url }}"&gt;{{ entry.title }}&lt;/a&gt; - {{ entry.date|date("j") }}{{ entry.date|date("S") }} {{ entry.date|date("F") }} {{ entry.date|date("Y")}} - {% for category in entry.genre %}{{ category.title }}{% endfor %}&lt;/h3&gt;
        &lt;/article&gt;

    {% else %}

        &lt;article&gt;
            &lt;h3&gt;&lt;a href="{{ entry.url }}"&gt;{{ entry.title }}&lt;/a&gt;&lt;/h3&gt;
        &lt;/article&gt;

    {% endif %}

{% endfor %}
</code></pre>

<p>Hope that makes sense! Any help will be appreciated.</p>

<entries><categories><ordering>
Loop through entries with custom ordering, showing one from each category then start loop again
# Loop through entries with custom ordering, showing one from each category then start loop again

(Asked by: username_0 on 2016-02-28)

I have a channel with entries that may or may not have a custom date field populated.
I'd like the display a list of entries, looping through each category, showing one entry from each category. Then just start the loop again.
Then, show the rest of the entries which don't have the date field populated. I guess they would just have to be just ordered by postDate, but still only showing one per category each loop.
So (assuming there are 3 categories in total)...

Entry - 4th April - Category 1
Entry - 6th April - Category 2
Entry - 8th April - Category 3
Entry - 5th April - Category 1 - start the loop through categories again
Entry - 7th April - Category 2
Entry - 9th April - Category 3
Entry - Category 1
Entry - Category 2
Entry - Category 3

Note that the dates appear jumbled, as the categories are the thing dictating the order. Date order is a factor, but secondary to the looping of the categories.
This is the code I have so far, all it does is output the entries but with the ones without date fields populated appearing first.
{% for entry in craft.entries.section('heroes').order('date') %}

    {% if entry.date|length %}

        
            {{ entry.title }} - {{ entry.date|date("j") }}{{ entry.date|date("S") }} {{ entry.date|date("F") }} {{ entry.date|date("Y")}} - {% for category in entry.genre %}{{ category.title }}{% endfor %}
        

    {% else %}

        
            {{ entry.title }}
        

    {% endif %}

{% endfor %}

Hope that makes sense! Any help will be appreciated.

**Tags:** entries, categories, ordering

---

## 1 answers

### Answer by username_1 on 2016-02-28. Score: 3

I'd suggest breaking your problem down into code which gathers the IDs of each of the types of entries first, then outputs them....roughly like this (very much untested!! - but should be enough to understand the approach):
{# Arrays to store the IDs of the entries as we find them #}
{% set noDateIDs = [] %}
{% set dateIDs = [] %}

{% for entry in craft.entries.section('heroes').order('date') %}

    {% if entry.date|length %}
        {% set dateIDs = dateIDs|merge([entry.id])} %}
        {# Or of course you could output the data you want here directly but I like to keep my building and output separate for clarity #}
    {% else %}
        {% set noDateIDs = noDateIDs|merge([entry.id])}  %}
    {% endif %}

{% endfor %}

{% set noDateIDsString = noDateIDs|join(', ') %}
{% set dateIDsString = dateIDs|join(', ')  %}

{# Now actually output the dated entries #}
{% for entry in craft.entries.section('heroes').id(noDateIDsString).find() %}

    
        {{ entry.title }} - {{ entry.date|date("j") }}{{ entry.date|date("S") }} {{ entry.date|date("F") }} {{ entry.date|date("Y")}} - {% for category in entry.genre %}{{ category.title }}{% endfor %}
    

{% endfor %}

{# Output the undated ones ordered by postDate #}
{% for entry in craft.entries.section('heroes').id(dateIDsString).order('postDate') %}

        
            {{ entry.title }}
        

{% endfor %}

This approach is very useful generally in Craft, and is documented really well here in 'Manipulating Craft's ElementCriteriaModel objects with Twig' by Jérôme Coupé of Webstoemp

> **Comments:**
> **username_1**: You *can* probably pass  the ids directly as an array rather than doing that string conversion, not sure on that...  
> **username_0**: Apologies, I rephrased my question. Your answer is great for weeding out the ones without dates but I still need to loop through the categories, first and foremost.  
> **username_1**: Just to finish off our slack convo it's easy to loop over categories.  Within you could e.g. loop dates and get the 1st entry for that cat on that date. I still don't think that's quite what you want - the better approach would be list all the events that are under a category by date, or  list events on a date by ctaegory.  Looping both categories and dates is tricky & I am not even convinced the output would make much sense from a presentation point of view so I'd advise talking to the client more and showing them more real world examples of where this might not make much sense.  Just my 2c!  
> **username_0**: Agreed. The output will make no sense and be unhelpful to the user. I'm pushing back to the client in the hope that they see sense! Still might have to do it though :/  

---

