Search result shows JSON code from blocks

Hello together!
I have integrated the search function from the cookbook:

It worked right away. However, type: blocks are also searched and JSON code appears in the search results:

[{"content":{"text":" Lorem ipsum dolor sit amet elementum por …

How and where do I adjust the code to remove JSON code from the results? I have not found a solution here in the forum.


  <?php foreach ($results as $result): ?>
    <a href="<?= $result->url() ?>">
      <?= $result->text() ?>
  <?php endforeach ?>



return function ($site) {

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

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


Additionally, I would like to show the number of search results:
“12 hits were found”
How do I read out the total number of hits found?

thank you!

You’d have to call toBlocks() on the field (or, to get an excerpt, $result->text()->toBlocks()->excerpt().

1 Like

Thank you very much! Your solution has helped me perfectly!

I’m sure my 2nd question went down:
How do I output the number of search results?
For example: “12 hits were found”

$resultCount = $results->count();
1 Like

Thank you! Sometimes the solution is so simple :wink: