<p>This should do what you want.</p>

<pre><code>{% set videos = craft.entries({
    section: 'articles',
    type: 'video',
    level: 2,
}) %}

{% set articleIds = [] %}
{% for video in videos %}
    {% set article = video.getParent() %}
    {% set articleIds = articleIds|merge([article.id]) %}
{% endfor %}

{% set articles = craft.entries({
    id: articleIds,
    fixedOrder: true,
    limit: 6
}) %}
</code></pre>

<p>You go through all the videos and collect the IDs of the parent article entries. Once you have the IDs, make sure to set <a href="https://craftcms.com/docs/templating/craft.entries#fixedOrder" rel="nofollow"><code>fixedOrder</code></a> in the final criteria model and the entries are retuned in the same order as the IDs in the <code>id</code> param.</p>

<p>Note that the <a href="https://craftcms.com/docs/templating/entrymodel#getParent" rel="nofollow"><code>getParent</code></a> function queries the database for each video in your section. You might want to consider caching that piece of code.</p>

<p>And you can also test if replacing the code in the for loop with the following performs better.</p>

<pre><code>{% set articleId = video.getAncestors(1).ids()[0] %}
{% set articleIds = articleIds|merge([articleId]) %}
</code></pre>

CC BY-SA 3.0
2016-07-20T02:17:39.553Z
Thanks a lot, Works great, I don't know that we can use fixedOrder to do that

and using the "id" to get the entries is really something, can't think up of this method at first.
postsrc
CC BY-SA 3.0
2016-07-19T23:51:20.153Z
2016-07-19T23:51:20.153Z
carlcs
<p>If I'm reading this correctly, you need two loops: one to loop over the parent entries in the section and then a second to get the parent's children. That'll give you the control you need.</p>

<p>Something like (untested):</p>

<pre><code>{% for parentArticle in craft.entries({ 
           section: 'articles', type: 'videoArticle', limit: 6
        }).order('postDate asc') 
 %}

 {# do what you want with the parent, just a heading and description here #}

 &lt;h2&gt;{{parentArticle.title}}&lt;/h2&gt;
  {{parentArticle.topLevelDescription}}

 {# loop over the children underneath the parent sorted by most recent #}

  &lt;ul&gt;
  {% for childArticle in parentArticle.children.order('postDate asc') %}
      &lt;li&gt;&lt;a href="{{childArticle.url}}"&gt;{{childArticle.title}}&lt;/a&gt;
  {% endfor %}
  &lt;/ul&gt;

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

CC BY-SA 3.0
2016-07-19T20:59:27.233Z
2016-07-19T20:59:27.233Z
RitterKnight
<p>Is it possible to order a section of entries by their child entries post date?</p>

<pre><code>{% for article in craft.entries({
    section: 'articles',
    type: 'videoArticle',
    limit: 6
}).getChildren().order('postDate asc') %}
    &lt;a href="{{ article.url }}"&gt;{{ article.title }}&lt;/a&gt;
{% endfor %}
</code></pre>

<p>Each article has 1 child under it. I want to filter the articles by the latest postDate of the children.</p>

CC BY-SA 3.0
2016-05-22T14:00:27.723Z
Can you provide a little more information? Is this section a structure?
Luke Pearce
CC BY-SA 3.0
2016-05-23T00:05:23.167Z
@LukePearce yes, i edited the description
postsrc
CC BY-SA 3.0
2016-05-23T10:52:14.127Z
I'm struggling to see why you have these entries set up in such a way. What are the child entries within each parent?
Luke Pearce
CC BY-SA 3.0
2016-05-23T11:45:36.410Z
@LukePearce each article have multiple sub sections/children

and I want to sort the article based on the children postDate
postsrc
CC BY-SA 3.0
2016-05-23T11:47:48.550Z
Article1 contains (video1, video2, video3) • Article2 contains (Video1, Video2). when i loop the article.getChildren.latest() It will give me the Latest video from Article1 but I want to order the result by video postDate
postsrc
CC BY-SA 3.0
2016-05-23T11:48:13.073Z
Ok, but what is the content of the child entries and what is their relationship to the articles? You may be able to set up your site structure differently to achieve what you want.
Luke Pearce
CC BY-SA 3.0
2016-05-23T11:50:32.317Z
For article It's just a short description of the topic and they have multiple sub section/children. I want to sort the article based on the children postDate. If I add new children for article2, article2 will be sorted first
postsrc
CC BY-SA 3.0
2016-05-23T11:50:48.303Z
Let us [continue this discussion in chat](http://chat.stackexchange.com/rooms/40164/discussion-between-luke-pearce-and-densityx).
Luke Pearce
<entries><ordering>
How can I order a sections entries by their children entry postDate?
# How can I order a sections entries by their children entry postDate?

(Asked by: username_0 on 2016-05-22)

Is it possible to order a section of entries by their child entries post date?
{% for article in craft.entries({
    section: 'articles',
    type: 'videoArticle',
    limit: 6
}).getChildren().order('postDate asc') %}
    {{ article.title }}
{% endfor %}

Each article has 1 child under it. I want to filter the articles by the latest postDate of the children.

**Tags:** entries, ordering

> **Comments:**
> **username_1**: Can you provide a little more information? Is this section a structure?  
> **username_0**: @LukePearce yes, i edited the description  
> **username_1**: I'm struggling to see why you have these entries set up in such a way. What are the child entries within each parent?  
> **username_0**: @LukePearce each article have multiple sub sections/children  and I want to sort the article based on the children postDate  
> **username_0**: Article1 contains (video1, video2, video3) • Article2 contains (Video1, Video2). when i loop the article.getChildren.latest() It will give me the Latest video from Article1 but I want to order the result by video postDate  
> **username_1**: Ok, but what is the content of the child entries and what is their relationship to the articles? You may be able to set up your site structure differently to achieve what you want.  
> **username_0**: For article It's just a short description of the topic and they have multiple sub section/children. I want to sort the article based on the children postDate. If I add new children for article2, article2 will be sorted first  
> **username_1**: Let us [continue this discussion in chat](http://chat.stackexchange.com/rooms/40164/discussion-between-luke-pearce-and-densityx).  

---

## 2 answers

### Answer by username_2 on 2016-07-19. Score: 3 | Accepted answer

This should do what you want.
{% set videos = craft.entries({
    section: 'articles',
    type: 'video',
    level: 2,
}) %}

{% set articleIds = [] %}
{% for video in videos %}
    {% set article = video.getParent() %}
    {% set articleIds = articleIds|merge([article.id]) %}
{% endfor %}

{% set articles = craft.entries({
    id: articleIds,
    fixedOrder: true,
    limit: 6
}) %}

You go through all the videos and collect the IDs of the parent article entries. Once you have the IDs, make sure to set fixedOrder in the final criteria model and the entries are retuned in the same order as the IDs in the id param.
Note that the getParent function queries the database for each video in your section. You might want to consider caching that piece of code.
And you can also test if replacing the code in the for loop with the following performs better.
{% set articleId = video.getAncestors(1).ids()[0] %}
{% set articleIds = articleIds|merge([articleId]) %}

> **Comments:**
> **username_0**: Thanks a lot, Works great, I don't know that we can use fixedOrder to do that  and using the "id" to get the entries is really something, can't think up of this method at first.  

---

### Answer by username_3 on 2016-07-19. Score: 1

If I'm reading this correctly, you need two loops: one to loop over the parent entries in the section and then a second to get the parent's children. That'll give you the control you need.
Something like (untested):
{% for parentArticle in craft.entries({ 
           section: 'articles', type: 'videoArticle', limit: 6
        }).order('postDate asc') 
 %}

 {# do what you want with the parent, just a heading and description here #}

 {{parentArticle.title}}
  {{parentArticle.topLevelDescription}}

 {# loop over the children underneath the parent sorted by most recent #}

  
  {% for childArticle in parentArticle.children.order('postDate asc') %}
      {{childArticle.title}}
  {% endfor %}
  

{% endfor %}


---

