Search returns empty content / filter only enabled toggle

Looking for way to get search results for enabled toggle in panel page.
Currently created something like this:

$results = $site->search(‘q’, [ ‘fields’ => [‘toggle’] ]);

But have two problems:

  1. Currently search returns all pages with toggle. Want to filter only those toggles which are switched to true.
  2. Search returns data with empty content. How can I get content and rest of data of each page?

Currently results looks like this (content and all other data are empty):

results: {
  data: {
    my-path-name1: {
      content: { },
      translations: null,
      children: {
        data: [ ]
      },
      drafts: null,
      searchHits: 6,
      searchScore: 6
    },
    my-pathname-2: {
      content: { },
      translations: null,
      children: {
        data: [ ]
      },
      drafts: null,
      searchHits: 6,
      searchScore: 6
    },
....

Any advice would be appreciated.

So the main question is - why search returns correct data structure, but data are empty?

The search request doesn’t seem to make sense, searching for a string ‘q’;

Where is the JSON generated? Could you please post your complete code?

The reason why the data is empty is because your are probably trying to encode the Kirby objects rather than an array.

I was putting * in place of ‘q’ too, but result was same (basically it works on any single string letter, to get those empty results). Im trying to search for all pages, which has toggle switched on.

Probably Im missing something… but how can I get those children with all data?

JSON generation looks something like this:

$json = array();
$json['search'] = $site->search('x', [ 'fields' => ['toggle'] ]);
$json = array_filter($json);
$preCachedData = json_encode($json, JSON_UNESCAPED_UNICODE);
echo $preCachedData;

Why do you use search when you want to filter by the value of a particular field?

$results = $site->index()->filterBy('toggle', true);

would make more sense in my opionion.

And then convert to array before encoding

$json = json_encode($results->toArray(), JSON_UNESCAPED_UNICODE)

Thank You, this is what I was looking for! Was trying filter, but without indexing…

One small question - maybe there is simpler way to get all necessary sub-data for images and files? Currently they are as array of pathname strings, therefore I do additional iteration to gather all image and file data.