So I have created a simple related articles collection, that will get all pages within the same ‘category’ excluding itself.
$related = page('blog')->children()->listed()->filterBy('category', $page->category(), ',');
$collection = $related->not($page->uri());
I want to add in a fallback to this so that if less than x pages have the same category, then fill up the remaining x with the latest articles.
So if i’ve set a limit of 4 related articles, and only 1 page has category of ‘web’, then fill the remaining 3 with latest articles.
Can someone point me in the right direction please? I’m guessing it’s an and/or type filterby statement?
texnixe
2
That would be something like this:
$count = $collection->count();
if ($count < 4) {
$limit = 4 - $count;
$latestArticles = page('blog')->articles()->sortBy('date', 'desc')->not($collection)->limit($limit);
$collection = $collection->add($latestArticles);
}
2 Likes
Thank you very much.
I ended up with
$related = page('blog')->children()->listed()->filterBy('category', $page->category(), ',');
$sorted = $related->sortBy('noteDate', 'desc', 'noteTime', 'desc')->not($page->uri())->limit(2);
$count = $sorted->count();
if ($count <= 2) {
$latest = page('blog')->children()->listed()->not($sorted);
$sortlatest = $latest->sortBy('noteDate', 'desc', 'noteTime', 'desc');
$sorted = $sorted->add($sortlatest)->not($page->uri())->limit(2);
}
Which seems to work as expected. Thanks for the help.