# Filtering multiple structure fields

**URL:** <https://forum.getkirby.com/t/filtering-multiple-structure-fields/6154>\
**Category:** Questions\
**Tags:** v2\
**Created:** [December 28, 2016, 10:19pm UTC](https://forum.getkirby.com/t/filtering-multiple-structure-fields/6154 "2016-12-28T22:19:09Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![taylorcammack](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/taylorcammack/32/466_2.png) [@taylorcammack](https://forum.getkirby.com/u/taylorcammack)\
**Post date:** [December 28, 2016, 10:19pm UTC](https://forum.getkirby.com/t/filtering-multiple-structure-fields/6154/1 "2016-12-28T22:19:09Z")

</div>

I’m pulling data from two structure fields (videos and products) and have merged it into an episodes structure field per [Fetch query from parent page - #2 by texnixe](https://forum.getkirby.com/t/fetch-query-from-parent-page/1290/2)

Here’s the episodes blueprint:

```
fields:
  gkm_episodes:
  label: Current Episodes
  type: structure
  style: table
  modalsize: large
  fields:
    name:
      label: Episode Title
      type: text

    episode:
      label: Associated Video
      type: episodes

    product:
      label: Associated Product
      type: products

    airdate:
      label: Air Date
      type: date
      format: MM/DD/YYYY

```

For each of the episodes, I’m trying to find the associated video and products. I tried something like this, but it isn’t filtering through the products structure to match the episode product with the product title.

```
<?php $episodes = $pages->find('episodes')->gkm_episodes()->toStructure() ?>
<?php $videos = $pages->find('videos')->gkm_videos()->toStructure() ?>
<?php $products = $pages->find('products')->gkm_products()->toStructure() ?>

<?php foreach($episodes as $episode): ?>

   <?php $productID = $episode->product(); ?>
   <?php $product = $products->filterBy('name', $productID); ?>
   
   <?= $product->name(); ?>

<?php endforeach ?>

```

Any ideas?

---

<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:** [December 28, 2016, 10:54pm UTC](https://forum.getkirby.com/t/filtering-multiple-structure-fields/6154/2 "2016-12-28T22:54:13Z")

</div>

I’m not quite sure I understand what you are trying to achieve here.

A little while ago, I posted a little plugin to filter by structure field values in this post, maybe it helps: [New Kirby Related Pages plugin](https://forum.getkirby.com/t/new-kirby-related-pages-plugin/6014/5)

---

<div class="post-metadata">

**Author:** ![taylorcammack](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/taylorcammack/32/466_2.png) [@taylorcammack](https://forum.getkirby.com/u/taylorcammack)\
**Post date:** [December 29, 2016, 12:27am UTC](https://forum.getkirby.com/t/filtering-multiple-structure-fields/6154/3 "2016-12-29T00:27:19Z")

</div>

Sorry – maybe this will help. Here’s the products blueprint.

```
title: Products
icon: cube
fields:
  gkm_products:
    label: Products
    type: structure
    entry: >
      <strong>{{name}}</strong>
    modalsize: large
    fields:
      name:
        label: Product Title
        type: text
      cover:
        label: Product Image
        type: image
        width: 1/2
      link:
        label: Store Link
        type: url
        width: 1/2
     description:
        label: Product Description
        type: textarea
     contents:
        label: Product Contents
        type: textarea

```

So for each of the episodes, I’ve designated a specific product. What I’m trying to do is filter through the `gkm_products` structure, finding the matching product, and then have access to the rest of the collection for that product:

`$product->name();`  
`$product->cover();`

I have previously accomplished this by matching the page uid like so:

```
<?php $productID = $episode->product(); ?>
<?php $product = page('products')->children()->findBy('uid', $productID); ?>

```

But I ended up making a lot of pages for data that would be better stored and accessed in a structure.

---

<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:** [December 29, 2016, 12:41am UTC](https://forum.getkirby.com/t/filtering-multiple-structure-fields/6154/4 "2016-12-29T00:41:24Z")

</div>

I see; I think `findBy()` will do the job here:

```auto
<?php $episodes = $pages->find('episodes')->gkm_episodes()->toStructure() ?>
<?php $videos = $pages->find('videos')->gkm_videos()->toStructure() ?>
<?php $products = $pages->find('products')->gkm_products()->toStructure() ?>

<?php foreach($episodes as $episode): ?>

   <?php $productID = $episode->product(); ?>
   <?php $product = $products->findBy('name', $productID); ?>
   
   <?= $product->name(); ?>

<?php endforeach ?>

```

---

<div class="post-metadata">

**Author:** ![taylorcammack](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/taylorcammack/32/466_2.png) [@taylorcammack](https://forum.getkirby.com/u/taylorcammack)\
**Post date:** [December 29, 2016, 4:24am UTC](https://forum.getkirby.com/t/filtering-multiple-structure-fields/6154/5 "2016-12-29T04:24:54Z")

</div>

I’ve tried that and I get:  
`Call to a member function name() on null.`

---

<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:** [December 29, 2016, 1:04pm UTC](https://forum.getkirby.com/t/filtering-multiple-structure-fields/6154/6 "2016-12-29T13:04:13Z")

</div>

Hm, there seems to be some sort of issue with the value returned from `$episode->product()`. It works if you either `trim()` it or use the `value()` method:

```auto
<?php $episodes = $pages->find('episodes')->gkm_episodes()->toStructure() ?>
<?php $videos = $pages->find('videos')->gkm_videos()->toStructure() ?>
<?php $products = $pages->find('products')->gkm_products()->toStructure() ?>

<?php foreach($episodes as $episode): ?>

   <?php $productID = $episode->product()->value(); ?>
   <?php $product = $products->findBy('name', $productID); ?>
   
   <?= $product->name(); ?>

<?php endforeach ?>

```

In any case, you should use an if statement to check if `$product()` exists before calling a method on it.

---

<div class="post-metadata">

**Author:** ![taylorcammack](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/taylorcammack/32/466_2.png) [@taylorcammack](https://forum.getkirby.com/u/taylorcammack)\
**Post date:** [December 29, 2016, 3:21pm UTC](https://forum.getkirby.com/t/filtering-multiple-structure-fields/6154/7 "2016-12-29T15:21:31Z")

</div>

Thanks! That worked. 👍

---

<div class="post-metadata">

**Author:** ![distantnative](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/distantnative/32/11319_2.png) [@distantnative](https://forum.getkirby.com/u/distantnative)\
**Post date:** [February 6, 2025, 4:53pm UTC](https://forum.getkirby.com/t/filtering-multiple-structure-fields/6154/8 "2025-02-06T16:53:09Z")

</div>

Archived on February 6, 2025, 4:53pm UTC


