# Error loading random image from specific file selector field on random page

**URL:** <https://forum.getkirby.com/t/error-loading-random-image-from-specific-file-selector-field-on-random-page/24900>\
**Category:** Questions\
**Tags:** v3\
**Created:** [March 5, 2022, 10:47pm UTC](https://forum.getkirby.com/t/error-loading-random-image-from-specific-file-selector-field-on-random-page/24900 "2022-03-05T22:47:07Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![michelin91](https://avatars.discourse-cdn.com/v4/letter/m/a88e4f/32.png) [@michelin91](https://forum.getkirby.com/u/michelin91)\
**Post date:** [March 5, 2022, 10:47pm UTC](https://forum.getkirby.com/t/error-loading-random-image-from-specific-file-selector-field-on-random-page/24900/1 "2022-03-05T22:47:07Z")

</div>

Hi,

I’m following Kirby’s [random content](https://getkirby.com/docs/cookbook/content/random-content) guide to fetch an array of 3 random images from random pages, however I’d like for the images to be from a specific file selector field instead of all images on that page.

```auto
<?php
    $references = page('exhibitions')
      ->children()
      ->references()
      ->shuffle()
      ->limit(3)
      ->toFiles();
    foreach($references as $reference): ?>
      <li>
        <img src="<?= $reference->url() ?>">
      </li>
    <?php endforeach ?>

```

In my code “references()” is the image selector field, which I’m using in place of all “images()”. For some reason this gives me an error, though when I use “images()” without the toFiles() method it works fine. I have images uploaded to the specified image selector field too, so that shouldn’t be an issue.

Any help would be enormously appreciated!

---

<div class="post-metadata">

**Author:** ![michelin91](https://avatars.discourse-cdn.com/v4/letter/m/a88e4f/32.png) [@michelin91](https://forum.getkirby.com/u/michelin91)\
**Post date:** [March 5, 2022, 11:23pm UTC](https://forum.getkirby.com/t/error-loading-random-image-from-specific-file-selector-field-on-random-page/24900/2 "2022-03-05T23:23:09Z")

</div>

Update: Is the problem actually with trying to fetch images from a files field on multiple pages?

I tried the following and it worked:

```auto
<?php
    $references = page('exhibitions')
      ->children()
      ->shuffle()
      ->first()
      ->references()->toFiles()
      ->shuffle()
      ->limit(3);
      foreach($references as $reference): ?>
        <li>
          <?= $reference->url() ?>
        </li>
      <?php endforeach ?>

```

But ideally I could get random images from the “references” field on all children pages of “exhibitions”…

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [March 5, 2022, 11:41pm UTC](https://forum.getkirby.com/t/error-loading-random-image-from-specific-file-selector-field-on-random-page/24900/3 "2022-03-05T23:41:33Z")

</div>

You cannot call a field on a collection of pages.

There are different ways to achieve this, either looping through the pages or filtering the files. Maybe the last example is the best.

```auto
<?php
$files = new Files();
$exhibitions = page('exhibitions')->children();
foreach ($exhibitions as $exhibition) {
  $files->add( $exhibition->references()->toFiles());
}

$files = $files->shuffle()->limit(3);
foreach($files as $reference): ?>
  <li>
    <img src="<?= $reference->url() ?>">
  </li>
<?php endforeach ?>

```

Alternatively:

```auto
$files = new Files();
$fields = page('exhibitions')->children()->pluck('references');
foreach ($fields as $field) {
  $files->add($field->toFiles());
}
dump($files);
// then as above

```

Or (same as first example, only using `map()` instead of the `foreach` loop):

```auto
$files = new Files();
page('exhibitions')->children()->map(fn ($child) => $files->add($child->references()->toFiles()));
$files = $files->shuffle()->limit(3);

```

In all three examples, we instantiate a new Files object and then loop through the pages/fields to add the files from the `references` field to this object.

Or filtering the files by whether or not they are contained in the field:

```auto
$files = page('exhibitions')->children()->files()->filter(fn ($file) => $file->parent()->references()->toFiles()->has($file))->shuffle()->limit(3);

```

If the files in your references field have a template assigned, you could first filter by that template:

```auto
$files = page('exhibitions')->children()->files()->template('yourTemplateName')->filter(fn ($file) => $file->parent()->references()->toFiles()->has($file))->shuffle()->limit(3);

```

---

<div class="post-metadata">

**Author:** ![michelin91](https://avatars.discourse-cdn.com/v4/letter/m/a88e4f/32.png) [@michelin91](https://forum.getkirby.com/u/michelin91)\
**Post date:** [March 6, 2022, 11:04am UTC](https://forum.getkirby.com/t/error-loading-random-image-from-specific-file-selector-field-on-random-page/24900/4 "2022-03-06T11:04:29Z")

</div>

Thanks @texnixe ! I had a feeling I didn’t quite understand fetching content from page collections yet; your thorough explanation with various approaches was really helpful. I ended up using the third example and it worked beautifully 🙂
