Get a page by field value (inside structured content)

This a follow up question to this one: How to pass a varaible between pages?

There is a collection template with this blueprint:

items:
  label: Items
  type: structure
  style: table
  entry:
    - item
  fields:
    item:
      label: Item
      type: select 
      options: query
      query:
        page: items
        fetch: children
        flip: true

In another template item, I would like to retrieve the first collection with a field item corresponding to the current page.

I tried something like:

<?  $item = $page->uid() ?>
<?=  $collection_default = page('collections')->children()->findBy('items', …)->first(); ?>

Is it possible to replace … with something (like $items[$item]) fetching this collection?

The filter function with a callback is the way to go here, because you can’t find the child in the collection directly by a value in a structure field.

<?php
$item = $page->uid();

$collection_default = page('collections')->children()->filter(function($child) use($item) {
  return $child->items()->structure()->findBy('item', $item);
})->first();
1 Like

This works fine, thank you!