Page collection : Random but not self

Hey all,

I’m currently developing an agency portfolio website where, on the projects pages, I’m displaying a random picked project at the end of the page. The idea is to have the user to be redirected to a random project in order to discover more about what the agency is doing.

The problem is that, sometimes, the displayed project is the same that we’re currently looking at …

Is there a way to “exclude self” so the random picked project is always another one than the current one?

Here’s the code I’m using for now:

{% set random = random(page.find('/projects').children().published()) %}

Thanks in advance

Try page.siblings() instead of page.find('/projects').children()

Thanks for you fast answer.

Sadly, it looks like it can still displays the current page …

Then maybe a bug, because it should not. According to docs

'@self.siblings' - A collection of all other pages on this level

Yes, from what I understand from the documentation, it shouldn’t displays the current page …

@self.siblings - All the sibling pages
This collection will collect all the published Pages at the same level of the current page, excluding the current page:

Maybe it’s due to the random() function …

random() cannot get a page and add it to collection. Could you debug siblings and see if current page is really there in the list? Is it possible you have a duplicate page which seems like the same page, but actually isn’t?

@Valord, It is often not appreciated if a post is cross-posted on another forum. It ultimately means the effort invested by people on one forum is wasted…

It would therefor be polite to pro-actively mention the post has been cross-posted.

1 Like

Sorry about that, I didn’t thought about it this way … I’ll delete my post on the other forum.

Thanks for your help, I just solved it with that code:

{% set random = random(page.collection({ items: '@self.siblings'}).published()) %}