How can i get last 2 records in kirby?

how can i get last 2 records in kirby
``$list= page(‘projects’)->children()->sortBy(‘sort’, ‘asc’)->limit(2);

this logic is not giving me desired result.

If you want to get the last two, you have to change the sorting order to desc (provided your projects use prepended numbers to sort their order).

I have two questions to that example.

1.)
Could it be possible to get the last Children / Records from the whole site?
This could be interessting in a blog page with more Categories (parents).

2.)
Is it possible to have the above query WITH the depency to a certain template or type of a page?
So get only the last 2 pages of type “blogarticle” from the whole site?

Thank you

You can filter by almost anything, including the intended or actually used template:

$list= page(‘projects’)->children()->filterBy('intendedTemplate', 'blogarticle')->sortBy(‘sort’, ‘asc’)->limit(2);

As regards the last question, what exactly want to fetch? The last x pages that were modified or the last x pages that were created? If the latter, do your pages all have a date field? If the first, you can sort by modified().

As the site i am working on is organized in 4-5 main categories with 2-3 subcategories and then Articles, i want to bring up the latest Articles across the wholes site.

So i thought it would be best to get the latest articles from template type “article”.

One thing you could do is filter the complete index:

$list= $site->index()->filterBy('intendedTemplate', 'article')->sortBy(‘sort’, ‘asc’)->limit(2);

Or, if the index contains many more pages, you could get the individual collections and merge them.

Yes, thats geil!
Thank you @texnixe