Get latest posts from different children of a page

HI!

I have a main page called “Products”. Under it are pages, eg: brand 1, brand 2, brand 3, etc.
Under these brands are posts.
So the structure is like:

      Products
         > Brand 1
               > post 1
               > post 2
               > ...
         > Brand 2
              > post 1
              ...
         ...

How would I get the latest posts from all the direct children of my main page Products? I’d like to display it at least 10 or so on my home page.

For example, all my new post is under Brand 2 then it will only display those from Brand 2. But if let say I have 2 new posts from Brand 1 and 3 new posts from Brand 2, etc. then it will show all these new posts.

For now I can only show latest posts from page(eg: Brand 1).

TIA

Hi there,

have a look at this page in the docs: https://getkirby.com/docs/cheatsheet/page/grand-children
You could also “foreach” through the “Brand” pages and get each children. Using ->grandChildren() is way easier though.

Cheers,
Thomas

Hi @thguenther

I have played with grandChildren before but my problem is how do I get the LATEST post from all of these grandchildren.
Is there an auto way for Kirby on knowing which of them are the latest?
I can get the latest post of a page using their numbers and using flip() but for my problem I can’t use it because I’m trying to get the grandchildren.
I hope you understand the problem.

Thanks.

Like this:

<?php
$latestPost = page('products')->grandchildren()->visible()->sortBy('date', 'desc')->limit(10);
?>

@texnixe

Is that “date” the date of posting? which btw I cannot see anywhere in the panel. Or do I have to create a field for a date?

The “date” field is a field you need to add to your blueprint. You can set it to override to “now” every time you save a post though: https://getkirby.com/docs/cheatsheet/panel-fields/date

Your code should look like that then:

  <?php foreach(page('products')->grandChildren()->sortBy('date', 'desc')->limit(10) as $post): ?>
    <a href="<?php echo $post->url() ?>">
      <?php echo html($post->title()) ?>
    </a>
  <?php endforeach ?>

No, I assumed you had a date field in your posts. Alternatively, you could use $page->modified() instead. But modified is updated whenever the page is saved.

@thguenther
@texnixe

Working! Thanks guys.

Hint:
Don’t call the field “date”, but call it e.g. “pagedate” or “created” or so on (like you want).

This helps you to avoid conficts with the Kirby funktion date, e.g at https://getkirby.com/docs/cheatsheet/page/date

Good luck!

Contrary to what @anon77445132 is suggesting, if you don’t need any other date fields, your life is much easier with a date field called “date”:

Returns a unix timestamp or formatted date string from the page’s date field or any other field containing a parsable date

So there is no reason whatsoever to call it anything else but “date”. You can, however, give it any name you like.

@anon77445132: Check out the blogarticle.txt files (/content/blog) of the getkirby.com website on GitHub for the use of date as date field.