# Query nested blocks fields

**URL:** <https://forum.getkirby.com/t/query-nested-blocks-fields/25367>\
**Category:** Questions\
**Tags:** v3\
**Created:** [April 29, 2022, 1:41pm UTC](https://forum.getkirby.com/t/query-nested-blocks-fields/25367 "2022-04-29T13:41:33Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jash](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/jash/32/2173_2.png) [@jash](https://forum.getkirby.com/u/jash)\
**Post date:** [April 29, 2022, 1:41pm UTC](https://forum.getkirby.com/t/query-nested-blocks-fields/25367/1 "2022-04-29T13:41:33Z")

</div>

Hey,

is it possible to query nested blocks fields within a (multiselect) field right in the blueprint? Here is a small example of three blocks fields which are nested. How to get all `thirdlevelblockentry` values into the multiselect field?

Thanks

```auto

fields:
    firstLevelBlocks:
        type: blocks
        fieldsets:
            secondLevelBlocks:
                type: blocks
                fieldsets:
                    thirdLevelBlocks:
                        type: block
                        fieldsets:
                            thirdlevelblockentry:
                                type: text

multiselectQuery:
    type: multiselect
    options: query
    query:
        fetch: site.find("page-with-nested-blocks").firstLevelBlocks...[how to continue this query (if possible)]
        text: "{{ block.thirdlevelblockentry }}"
        value: "{{ block.thirdlevelblockentry }}"

```

---

<div class="post-metadata">

**Author:** ![jash](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/jash/32/2173_2.png) [@jash](https://forum.getkirby.com/u/jash)\
**Post date:** [April 29, 2022, 2:15pm UTC](https://forum.getkirby.com/t/query-nested-blocks-fields/25367/2 "2022-04-29T14:15:03Z")

</div>

Here is a solution using Kirby Collections. But still would be interesting to see, if you can do the query totally within the blueprint, without the need of a collection.

Put this collection definition under `site/collections/my-nested-blocks.php`:

```auto
<?php
    return function ($site) {
        $collection = new \Kirby\Cms\Collection();
        $firstLevelBlocks = $site->find('page-with-nested-blocks')->firstLevelBlocks()->toBlocks();
        foreach ($firstLevelBlocks as $firstLevelBlock) {
            foreach ($firstLevelBlock->secondLevelBlocks()->toBlocks() as $secondLevelBlock) {
                 foreach ($secondLevelBlock->thirdLevelBlocks()->toBlocks() as $thirdLevelBlock) {
                     $collection->add($thirdLevelBlock);
                 }
            }
        }
        return $collection;
    };

```

In your blueprint use it like this:

```auto
multiselectQuery:
    type: multiselect
    options: query
    query:
        fetch: kirby.collection("my-nested-blocks")
        text: "{{ block.thirdlevelblockentry }}"
        value: "{{ block.thirdlevelblockentry }}"

```

---

<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:** [April 29, 2022, 2:33pm UTC](https://forum.getkirby.com/t/query-nested-blocks-fields/25367/3 "2022-04-29T14:33:31Z")

</div>

No, you can’t use loops (or any other complicated logic) in query language.
