# Search - Show Results from a Filter on a Page Only

**URL:** <https://forum.getkirby.com/t/search-show-results-from-a-filter-on-a-page-only/28133>\
**Category:** Questions\
**Tags:** v3\
**Created:** [March 17, 2023, 3:26pm UTC](https://forum.getkirby.com/t/search-show-results-from-a-filter-on-a-page-only/28133 "2023-03-17T15:26:26Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![nomad](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/nomad/32/12863_2.png) [@nomad](https://forum.getkirby.com/u/nomad)\
**Post date:** [March 17, 2023, 3:26pm UTC](https://forum.getkirby.com/t/search-show-results-from-a-filter-on-a-page-only/28133/1 "2023-03-17T15:26:26Z")

</div>

I have an Items page that has a custom controller to filter an item to only show those details for the one item (like a product detail view).

How do I make the search only show results for those “detail pages”?

---

<div class="post-metadata">

**Author:** ![nomad](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/nomad/32/12863_2.png) [@nomad](https://forum.getkirby.com/u/nomad)\
**Post date:** [March 17, 2023, 3:27pm UTC](https://forum.getkirby.com/t/search-show-results-from-a-filter-on-a-page-only/28133/2 "2023-03-17T15:27:24Z")

</div>

```auto
<?php

return function ($page) {
    $filter = get('filter');
    $items = $page->items()->toStructure();
    $selectedItem = $filter ? $items->findBy('itemnumber', $filter) : null;

 return [
        'filter' => $filter,
        'items' => $items,
        'selectedItem' => $selectedItem
    ];
};

```

---

<div class="post-metadata">

**Author:** ![nomad](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/nomad/32/12863_2.png) [@nomad](https://forum.getkirby.com/u/nomad)\
**Post date:** [March 17, 2023, 3:38pm UTC](https://forum.getkirby.com/t/search-show-results-from-a-filter-on-a-page-only/28133/3 "2023-03-17T15:38:00Z")

</div>

Page Template

```auto
<?php snippet('head') ?>

<?php snippet('header') ?>

<div id="content_container">
	<div class="grid-x grid-margin-x">
		<?php if ($filter && $selectedItem): ?>
			<div id="page_title" class="cell">
				<?= $page->meta_title() ?> - Item#<?= $selectedItem->itemnumber() ?>
			</div>
		<?php else: ?>
			<div id="page_title" class="cell">
				<?= $page->meta_title() ?>
			</div>
		<?php endif ?>

		<div id="page_content" class="cell">
			<div class="grid-x grid-margin-x">
			<?php if ($filter && $selectedItem): ?>
				<div id="selected_item" class="cell">
					<div id="item_image">
						<a href="<?= $selectedItem->image()->toFile()->url() ?>" data-lightbox="<?= $selectedItem->itemnumber() ?>">
							<?= $selectedItem->image()->toFile() ?>
						</a>
						<span><i class="fa-solid fa-magnifying-glass-plus"></i> Click on image to enlarge.</span>
					</div>
					<div id="item_details">
						<div id="item_number">
							Item# <?= $selectedItem->itemnumber() ?>
						</div>
						<div id="item_full_description">
							<?= $selectedItem->fulldescription() ?>
						</div>
						<div id="item_price">
							Price: $<?= $selectedItem->price() ?>
						</div>
					</div>
				</div>

			<?php else: ?>

				<div id="page_sidebar" class="cell medium-3">
					<h2 id="submenu_header">
						<center><span><i class="fa-solid fa-rectangle-list fa-xl"></i></span> Categories</center>
					</h2>
					<?php snippet('submenu') ?>
				</div>
				<div class="cell medium-9">
					<?php if ($page->alert()->isNotEmpty()): ?>
						<div id="page_alert">
							<?= $page->alert()->kt() ?>
						</div>
					<?php endif ?>

					<table id="items_listing">
						<tbody>
						<?php foreach ($items as $item): ?>
						<!-- show the list in the table here -->
							<tr class="list-item-row">
								<td class="list-item">
									<a href="?filter=<?= $item->itemnumber() ?>"><span><i class="fa-solid fa-magnifying-glass-plus"></i></span><?= $item->image()->tofile() ?></a>
									<span class="list-item-number"><?= $item->itemnumber() ?></span>
									<?php if ($item->newitemdate()->isNotEmpty()): ?>
										<div class="list-item-date">
											New <?= $item->newitemdate()->toDate('m-d-y') ?>
										</div>
									<?php endif ?>
								</td>
								<td class="list-item-short-desc">
									<?= $item->shortdescription() ?>
									<span class="list-item-price">$<?= $item->price() ?></span>
								</td>
							</tr>
								
						<?php endforeach ?>
						</tbody>
					</table>

					<div class="button-group align-center">
						<a class="brand-button" href="/table-of-contents">Table of Contents</a>

						<?php if ($page->hasPrevListed()): ?>
							<a class="brand-button" href="<?= $page->prevListed()->url() ?>">Previous Section</a>
							<?php endif ?>

							<?php if ($page->hasNextListed()): ?>
							<a class="brand-button" href="<?= $page->nextListed()->url() ?>">Next Section</a>
						<?php endif ?>
					</div>
				</div>
			<?php endif ?>
			</div>
		</div>
	</div>
</div>

<?php snippet('footer') ?>

```

---

<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, 4:36pm UTC](https://forum.getkirby.com/t/search-show-results-from-a-filter-on-a-page-only/28133/4 "2023-03-17T16:36:00Z")

</div>

Could you please describe what exactly is not working as expected?

---

<div class="post-metadata">

**Author:** ![nomad](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/nomad/32/12863_2.png) [@nomad](https://forum.getkirby.com/u/nomad)\
**Post date:** [March 17, 2023, 4:51pm UTC](https://forum.getkirby.com/t/search-show-results-from-a-filter-on-a-page-only/28133/5 "2023-03-17T16:51:41Z")

</div>

Currently the site search only will show the content pages based on “title|text”. I need it to show the results of the filtered items only.

Example: /search?q=4437

I need it to search only for $selectedItem-\>itemnumber()

---

<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, 4:56pm UTC](https://forum.getkirby.com/t/search-show-results-from-a-filter-on-a-page-only/28133/6 "2023-03-17T16:56:26Z")

</div>

Don’t see how this search query relates to your code above?

---

<div class="post-metadata">

**Author:** ![nomad](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/nomad/32/12863_2.png) [@nomad](https://forum.getkirby.com/u/nomad)\
**Post date:** [March 17, 2023, 5:17pm UTC](https://forum.getkirby.com/t/search-show-results-from-a-filter-on-a-page-only/28133/7 "2023-03-17T17:17:29Z")

</div>

That is true. I was just showing the filtered page code to see what is needed for the search controller to function the way that I need.

Current Controller (how to modify?)

```auto
<?php

return function ($site) {

  $query = get('q');
  $results = $site->search($query, 'title|text|itemnumber');

  return [
    'query' => $query,
    'results' => $results,
  ];

};

```

---

<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, 5:47pm UTC](https://forum.getkirby.com/t/search-show-results-from-a-filter-on-a-page-only/28133/8 "2023-03-17T17:47:09Z")

</div>

`itemnumber` is not a field, but a subfield of the structure field, so limiting to itemnumber doesn’t make sense here. If you only want to get a particular structure field item, site search is not really the right choice, or you would have to search in the parent field, then if the query string is such a number and that query returns a result, filter the structure of the given page by that itemnumber, or something like that.

---

<div class="post-metadata">

**Author:** ![nomad](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/nomad/32/12863_2.png) [@nomad](https://forum.getkirby.com/u/nomad)\
**Post date:** [March 17, 2023, 5:56pm UTC](https://forum.getkirby.com/t/search-show-results-from-a-filter-on-a-page-only/28133/9 "2023-03-17T17:56:07Z")

</div>

How would you search in the parent field? Is it at the $site or $page level?

I assume it would be at the $page level, if so then I would need a function for each page with the list of items in the structured field.

---

<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, 6:16pm UTC](https://forum.getkirby.com/t/search-show-results-from-a-filter-on-a-page-only/28133/10 "2023-03-17T18:16:30Z")

</div>

It might make more sense to use subpages instead of the structure field. What was your reason for the structure field instead of subpages?

---

<div class="post-metadata">

**Author:** ![nomad](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/nomad/32/12863_2.png) [@nomad](https://forum.getkirby.com/u/nomad)\
**Post date:** [March 17, 2023, 6:18pm UTC](https://forum.getkirby.com/t/search-show-results-from-a-filter-on-a-page-only/28133/11 "2023-03-17T18:18:38Z")

</div>

The reason was so I could filter results to show a dynamic detail page without have to create double the amount of pages.

It also makes it easier for the client to only enter in an item once.

---

<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, 6:26pm UTC](https://forum.getkirby.com/t/search-show-results-from-a-filter-on-a-page-only/28133/12 "2023-03-17T18:26:04Z")

</div>

The maybe create virtual pages from the structure items.

---

<div class="post-metadata">

**Author:** ![nomad](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/nomad/32/12863_2.png) [@nomad](https://forum.getkirby.com/u/nomad)\
**Post date:** [March 17, 2023, 6:29pm UTC](https://forum.getkirby.com/t/search-show-results-from-a-filter-on-a-page-only/28133/13 "2023-03-17T18:29:53Z")

</div>

Okay. Resource to explain how to do that?

---

<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, 6:32pm UTC](https://forum.getkirby.com/t/search-show-results-from-a-filter-on-a-page-only/28133/14 "2023-03-17T18:32:59Z")

</div>

We have a complete guide with multiple examples, for virtual children from the structure field, you would have to adapt this a little.

> **[Virtual pages](https://getkirby.com/docs/guide/virtual-pages)**
>
> In Kirby, you can integrate data from any data source and use them as if they were real Kirby pages, including creating and editing their content in the Panel.

The principle is the same, you would create a model for the category page and the structure field would fill the virtual pages.

---

<div class="post-metadata">

**Author:** ![nomad](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/nomad/32/12863_2.png) [@nomad](https://forum.getkirby.com/u/nomad)\
**Post date:** [March 17, 2023, 6:50pm UTC](https://forum.getkirby.com/t/search-show-results-from-a-filter-on-a-page-only/28133/15 "2023-03-17T18:50:47Z")

</div>

The model would be within the route within config correct? Can you help me with an example I can modify?

---

<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, 6:56pm UTC](https://forum.getkirby.com/t/search-show-results-from-a-filter-on-a-page-only/28133/16 "2023-03-17T18:56:19Z")

</div>

No, route and model are two different things.

Use this example, but instead of reading from the .csv, get the structure field instead:

> **[Content from a spreadsheet](https://getkirby.com/docs/guide/virtual-pages/content-from-csv)**
>
> Load data from a CSV file and create virtual pages for each row.

Example with structure here in the first post: [Page model for virtual children pages on the fly](https://forum.getkirby.com/t/page-model-for-virtual-children-pages-on-the-fly/21798)

---

<div class="post-metadata">

**Author:** ![nomad](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/nomad/32/12863_2.png) [@nomad](https://forum.getkirby.com/u/nomad)\
**Post date:** [March 17, 2023, 7:17pm UTC](https://forum.getkirby.com/t/search-show-results-from-a-filter-on-a-page-only/28133/17 "2023-03-17T19:17:49Z")

</div>

I have this as the model but it is giving a 404 error.

```auto
<?php

use Kirby\Cms\Page;

class ItemsPage extends Page
{

    public function children()
    {
        $itemdetail = [];

        foreach ($this->items()->toStructure() as $item) {
            $itemdetail[] = [
                'slug' => $this->slug() . $item->itemnumber(),
                'template' => 'item',
                'model' => 'item',
                'content' => [
                    'title' => $this->title() . '(' . $item->itemnumber() . ')',
                ]
            ];
        }

        return Pages::factory($itemdetail, $this);
    }

}

```

---

<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, 7:32pm UTC](https://forum.getkirby.com/t/search-show-results-from-a-filter-on-a-page-only/28133/18 "2023-03-17T19:32:37Z")

</div>

Just to make sure the model name is correct, so the page with the structure field uses the `items` template?

```auto
   public function children()
    {
        if ($this->children instanceof Pages) {
            return $this->children;
        }

        $itemdetail = [];

        foreach ($this->items()->toStructure() as $item) {
            $itemdetail[] = [
                'slug' => $item->itemnumber(),
                'template' => 'item',
                'model' => 'item',
                'content' => [
                    'title' => $this->title() . ' (' . $item->itemnumber() . ')',
                ]
            ];
        }

        return $this->children = Pages::factory($itemdetail, $this);
    }

```

---

<div class="post-metadata">

**Author:** ![nomad](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/nomad/32/12863_2.png) [@nomad](https://forum.getkirby.com/u/nomad)\
**Post date:** [March 17, 2023, 7:41pm UTC](https://forum.getkirby.com/t/search-show-results-from-a-filter-on-a-page-only/28133/19 "2023-03-17T19:41:17Z")

</div>

The page with the structure field uses the template ‘sidemenuproducts.php’.

---

<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, 7:41pm UTC](https://forum.getkirby.com/t/search-show-results-from-a-filter-on-a-page-only/28133/20 "2023-03-17T19:41:55Z")

</div>

Then you have to prepend this to `Page` and not `Items`

[Next page](https://forum.getkirby.com/t/search-show-results-from-a-filter-on-a-page-only/28133.md?page=2)
