Search Form

To add a search form to your website, create an HTML form which submits to a /search/results page, passing the search query as a q query string parameter.

<form action="{{ url('search/results') }}">
  <input type="search" name="q" aria-label="Search" placeholder="Search">
  <button type="submit">Go</button>

Then create a search/results.twig template, which will pass the q query string value into an entry query’s search parameter.

<h1>Search Results</h1>

{% set searchQuery ='q') %}
{% set entries = craft.entries()
  .all() %}

{% if entries|length %}
  <p>{{ entries|length }} results:</p>

    {% for entry in entries %}
      <li><a href="{{ entry.url }}">{{ entry.title }}</a></li>
    {% endfor %}
{% else %}
  <p>Your search for “{{ searchQuery }}” didn’t return any results.</p>
{% endif %}

Applies to Craft CMS 3.