Search inside a page

Hi everyone,

Is it possible to search for field(s) value(s) in a specific page, not searching pages by field value ?

Thank you

Are you using Kirby 2 or 3? In general, yes you can. If you look at this cookbook guide, the search will run on whatever you feed the $results variable:

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

You just need to amend that to point to a specific page and add the fields you want to search. I often do this so the search only searches the blog section of a site, for example

If you changed it to:

$results = $site->find('yourpage')->search($query, 'title|text|yourfield|myfield');

It should only search that page.

However, this will cause a page refresh to get the results which you probably don’t want on a single page search. It might be better to Ajax the results or use something like Tipue or ListJS.

Please always choose the right question category (Kirby 2 or 3) so we know immediately what we are dealing with and can help you faster. Thank you. The “General” category is not for questions.

What is your expected result? The name of the field? Or just a bool that tells you if the page contains that value?

Kirby’s search methods always search within a collection, the result is always a pages collection.

You could however, create a custom search that searches within the content object (`$page->content()), i.e. loop through all fields and return the field(s) that contain the search string, or search the raw data.

Thanks for your answers.

In the end, I just want to verify if a page has a tag value or not.
So I guess @texnixe advice about going through $page->content() will do the trick.
I’ll post a custom page method if I succeed doing it!

@julieng If you just want to check if the tags field has a given value, then you don’t even need a search function:

$searchTag = 'travel';
if(in_array($searchTag, $page->tags()->split(','))) {
  echo "Yeah, found!";