"array_rand(): Argument #1 ($array) cannot be empty" error when using collection with unpublished posts

I’m experiencing a specific issue with Grav collections that seems to be triggered by the number of unpublished posts.

Template code:

{% set collection = blog.collection().published(true).order('date', 'desc') %}

{% for post in collection.slice(0, 1) %}

<!-- First post display -->

{% endfor %}

{% for post in collection.slice(1, 3) %}

<!-- Next 3 posts display -->

{% endfor %}

The pattern I’ve discovered:

  • 6 posts total: Works fine with 1-2 unpublished posts, fails with 3+ unpublished posts
  • 8 posts total: Works fine with 1-2 unpublished posts, fails with 3+ unpublished posts
  • The error occurs regardless of the page limit configuration (tested with limit: 6 and limit: 8)

Error details:

array_rand(): Argument #1 ($array) cannot be empty

Location: /system/src/Grav/Common/Iterator.php in the random() method

What I’ve tried that DOESN’T work:

  • Using blog.children.published(true).order(‘date’, ‘desc’) - same error
  • Adding {% if collection|length > 0 %} checks
  • Increasing the page limit configuration

My blog page configuration:

title: Blog
content:
    items:
        - '@self.children'
    limit: 6
    order:
        by: date
        dir: desc
    pagination: true
    url_taxonomy_filters: true

Questions:

  1. Why does reusing a collection variable cause this error when having 3+ unpublished posts?
  2. Is this related to iterator consumption when slicing collections multiple times?
  3. Could this be a bug in how Grav’s Iterator handles multiple slice operations?

Additional info:

  • I’m not explicitly calling any random() methods in my template
  • The error appears to be triggered internally by Grav’s collection processing
  • The issue is reproducible and consistent with the 3+ unpublished posts threshold
  • The problem seems specifically related to reusing the same collection variable multiple times

Environment:

  • Grav version: v1.7.49.5
  • PHP version: 8.1.33

Any insights would be greatly appreciated!

UPDATE - PROBLEM ISOLATED: I’ve successfully isolated the issue to a specific theme.

What I tested: I created a simple modular template in the Quark theme using the exact same collection logic:

twig

{% set collection = blog.collection().published(true).order('date', 'desc') %}
{% for post in collection.slice(0, 1) %}...{% endfor %}
{% for post in collection.slice(1, 3) %}...{% endfor %}

Results with Quark theme:

  • :white_check_mark: Works perfectly with all posts published
  • :white_check_mark: Works perfectly with some posts unpublished
  • :white_check_mark: Works perfectly even with zero published posts
  • :white_check_mark: No array_rand() errors at all

Conclusion: The issue is not a core Grav problem but something specific to the Mundana theme I was originally using. The collection slicing logic itself is sound.

Next steps: I’ll need to debug what’s different in the Mundana theme that’s causing this error. It could be:

  • Theme-specific collection processing
  • Additional theme code that calls random() methods
  • Theme configuration or overrides affecting collections
  • Conflicting theme plugins or dependencies

Thanks to everyone who helped think through this. The good news is it’s not a Grav core bug, just something theme-specific to track down.

For anyone using Mundana theme: Be aware this collection pattern may cause issues depending on your setup.

You are in the sidebar: /templates/partials/sidebar/categories.html.twig#L25, which probably warrants the {% if collection|length > 0 %} to check if both terms and taxonomy.findTaxonomy({'category' : term}) returns more than 0.

1 Like

Thank you @OleVik. I think it has hit the nail on the head, because I have tried to disable the categories in the sidebar and there is no problem. I will look for a way to improve this partial template