On an article page i show 3 random entries, and a load more button via sprig.

{% set query = craft.entries().section(section).orderby('RAND()').offset(offset).limit(3) %}

{% set items = query.all() %}

Pressing the load more button shows 3 more Entries.

Now i get duplicate entries. How can i prevent this and exclude the already shown entries?

As described here in video 2 in the sprig documentation, if i have only one random article and want to reload the component and show another random article, i can exclude the last shown entry in the query.

set a last entry id:

{% set lastEntryId = lastEntryId ?? 0 %}

in the link/button to get the next random entry, set the value:

s-val:last-entry-id="{{ entry.id }}"

and in the query i exclude the last used id:

{% set query = craft.entries().section(section).id('not ' ~ lastEntryId).orderby('RAND()').offset(offset).limit(3) %}

How can i adopt this to my setup? I guess, get the already 3 first shown entries and put them in array and exclude these ids in the query (how do i do that?) and update this array after showing more entries with the load more button.

1 Answer 1


You answered your own question, you just need a list of IDs to exclude rather than a single ID.

Here's an example in which the incoming IDs are split and merged into the id query parameter to filter out the entry IDs.

The join filter is then used to output the merged last IDs with the IDs from the query.

{% set lastEntryIds = lastEntryIds ?? '' %}
{% set lastEntryIds = lastEntryIds|split(',') %}

{% set query = craft.entries()

{% set items = query.all() %}
{% set ids = query.ids() %}

{{ hiddenInput('lastEntryIds', lastEntryIds|merge(ids)|join(',')) }}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.