2

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') %}
    <a href="{{ article.url }}">{{ article.title }}</a>
{% endfor %}

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

  • 1
    Can you provide a little more information? Is this section a structure? – Luke Pearce May 22 '16 at 14:00
  • @LukePearce yes, i edited the description – densityx May 23 '16 at 0:05
  • 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 May 23 '16 at 10:52
  • @LukePearce each article have multiple sub sections/children and I want to sort the article based on the children postDate – densityx May 23 '16 at 11:45
  • 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 – densityx May 23 '16 at 11:47
3

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]) %}
  • 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. – densityx Jul 20 '16 at 2:17
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 #}

 <h2>{{parentArticle.title}}</h2>
  {{parentArticle.topLevelDescription}}

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

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

{% endfor %}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for?Browse other questions tagged or ask your own question.