Sort Order with grandChildren

Hi there,

I use this Code (snippet) for my blog “Home”:

<?php $articles = page('blog')->grandChildren()->visible()->flip()->paginate(5) ?>

For your understanding I have following site structure:

-Blog (home)
|-Blog Category I
|--Article I
|--Article II
|-Blog Catorgory II
|--Article I
|--Article II

So, what I want is:

On my Blog page show all articles of the subPages. There for I use the code mentioned above. On each Category Page I use this code:

<?php $articles = $page->children()->visible()->flip()->paginate(5) ?>

Both codes work but one exception:

The sort order for my blog page Home do not sort in the correct order. I can not say in which order it get sorted but not the newest first. I think it first sort each category and then within each category it sort newest first.

Is there a solution for sorting all grandChildren Pages newest first?

Thanks in advance


You can use sortBy():

<?php $articles = page('blog')->grandChildren()->visible()->sortBy('date', 'desc')->paginate(5) ?>
1 Like

Oh that was easy … :slight_smile:

Thanks works fine!