# Querying tags from a structured field in sibling pages?

**URL:** <https://forum.getkirby.com/t/querying-tags-from-a-structured-field-in-sibling-pages/28131>\
**Category:** Questions\
**Tags:** v3\
**Created:** [March 16, 2023, 4:40pm UTC](https://forum.getkirby.com/t/querying-tags-from-a-structured-field-in-sibling-pages/28131 "2023-03-16T16:40:50Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![piaaaac](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/piaaaac/32/5568_2.png) [@piaaaac](https://forum.getkirby.com/u/piaaaac)\
**Post date:** [March 16, 2023, 4:40pm UTC](https://forum.getkirby.com/t/querying-tags-from-a-structured-field-in-sibling-pages/28131/1 "2023-03-16T16:40:50Z")

</div>

I have a tag field called `textLabel` inside a structure field called `tableTexts`.  
I am trying to get the tag field’s suggested options using all values from this same tag field, from all pages with the same template named `project`

```auto
fields:
  tableTexts:
    type: structure
    fields: 
      textLabel:
        type: tags
        max: 1
        options: 
          type: query
          # fetch ... 

      textContent:
        type: textarea

```

My two best shots are the following, but I get errors (let me know if the error messages are helpful)

(A)

```auto
          fetch: site.pages.index.template('project').tableTexts.toStructure
          text: "{{ structureItem.textLabel }}"
          value: "{{ structureItem.textLabel }}"

```

(B)

```auto
          fetch: site.pages.index.template('project')
          text: "{{ item.tableTexts.toStructure.pluck('textLabel', ',', true) }}"
          value: "{{ item.tableTexts.toStructure.pluck('textLabel', ',', true) }}"

```

---

<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 16, 2023, 4:52pm UTC](https://forum.getkirby.com/t/querying-tags-from-a-structured-field-in-sibling-pages/28131/2 "2023-03-16T16:52:01Z")

</div>

None of this will work. You would need a custom method that fetches all the values of all pages and fields into a single collection/array

---

<div class="post-metadata">

**Author:** ![piaaaac](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/piaaaac/32/5568_2.png) [@piaaaac](https://forum.getkirby.com/u/piaaaac)\
**Post date:** [March 16, 2023, 7:37pm UTC](https://forum.getkirby.com/t/querying-tags-from-a-structured-field-in-sibling-pages/28131/3 "2023-03-16T19:37:52Z")

</div>

Where should this method be implemented?  
I’m guessing in a page model, but the tags of each page would still have to be merged into a single array.

---

<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 16, 2023, 7:41pm UTC](https://forum.getkirby.com/t/querying-tags-from-a-structured-field-in-sibling-pages/28131/4 "2023-03-16T19:41:00Z")

</div>

Yes, page model or a custom page method. Basically, you would loop through the children and fetch all textLabel values into an array.

---

<div class="post-metadata">

**Author:** ![piaaaac](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/piaaaac/32/5568_2.png) [@piaaaac](https://forum.getkirby.com/u/piaaaac)\
**Post date:** [March 16, 2023, 7:49pm UTC](https://forum.getkirby.com/t/querying-tags-from-a-structured-field-in-sibling-pages/28131/5 "2023-03-16T19:49:21Z")

</div>

Page model

```auto
class ProjectPage extends Page {
  public function getPageTableLabels () {
    $pageLabels = [];
    foreach ($this->tableTexts()->toStructure() as $row) {
      $pageLabels = array_merge($row->textLabel()->split());
    }
    return $pageLabels;
  }
}

```

In the blueprint

```auto
textLabel:
  type: tags
  max: 1
  options: 
    type: query
    fetch: site.pages.index.template('project')
    text: "{{ item.getPageTableLabels.split }}"
    value: "{{ item.getPageTableLabels.split }}"

```

I get no errors but no suggestions either

---

<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 16, 2023, 7:53pm UTC](https://forum.getkirby.com/t/querying-tags-from-a-structured-field-in-sibling-pages/28131/6 "2023-03-16T19:53:34Z")

</div>

You have to use the model method in the query, not the text/value. BTW. its

```auto
type: query
query: ....

```

No longer `fetch`, when you use the new syntax. But I thought you wanted to get the values of all siblings, not just the current page?

---

<div class="post-metadata">

**Author:** ![piaaaac](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/piaaaac/32/5568_2.png) [@piaaaac](https://forum.getkirby.com/u/piaaaac)\
**Post date:** [March 16, 2023, 8:47pm UTC](https://forum.getkirby.com/t/querying-tags-from-a-structured-field-in-sibling-pages/28131/7 "2023-03-16T20:47:44Z")

</div>

Yes, that’s what I’m trying to do.  
With this setup (using page model) I don’t understand how to unite the arrays I get for each page into a single one.  
(There’s probably something I don’t fully get about the query language)

---

<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 16, 2023, 10:08pm UTC](https://forum.getkirby.com/t/querying-tags-from-a-structured-field-in-sibling-pages/28131/8 "2023-03-16T22:08:06Z")

</div>

```auto
    public function getPageTableLabels () {
        $pageLabels = [];
        foreach ($this->siblings() as $p) {
            foreach ($p->tableTexts()->toStructure() as $label)
            $pageLabels[] = $label->textLabel();
        }
        return $pageLabels;
    }

```

options:  
type: query  
query: page.getPageTableLabels  
text: “{{ item }}”  
value: “{{ item }}”

```auto

```

---

<div class="post-metadata">

**Author:** ![piaaaac](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/piaaaac/32/5568_2.png) [@piaaaac](https://forum.getkirby.com/u/piaaaac)\
**Post date:** [March 17, 2023, 10:39am UTC](https://forum.getkirby.com/t/querying-tags-from-a-structured-field-in-sibling-pages/28131/9 "2023-03-17T10:39:14Z")

</div>

Thanks something similar worked for me.  
For some reason I didn’t think I could get data from all pages directly in the page model — I was assuming methods in the page model would have to deal with this page’s data only.  
–  
Is there a more general place (apart from page models) where I can put a custom method to then use in blueprints (with Query Language)? ‘General’ meaning not relative to a specific template.

---

<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 17, 2023, 10:48am UTC](https://forum.getkirby.com/t/querying-tags-from-a-structured-field-in-sibling-pages/28131/10 "2023-03-17T10:48:08Z")

</div>

Yes, custom methods as I mentioned above, in this case a custom page method, or maybe a site method.

> **[Page methods](https://getkirby.com/docs/reference/plugins/extensions/page-methods)**
