Skip to content

Instantly share code, notes, and snippets.

@ben-rogerson
Created October 11, 2018 23:34
Show Gist options
  • Save ben-rogerson/b1ba6889448dc1b0cceedf36c8e6804f to your computer and use it in GitHub Desktop.
Save ben-rogerson/b1ba6889448dc1b0cceedf36c8e6804f to your computer and use it in GitHub Desktop.
How to include search queries into your Craft searches
{% set activeQuery = craft.app.request.getParam('q')|default('') %}
{% set searchResults = activeQuery ?
craft.products({
search: {
query: activeQuery,
subRight: true,
},
order: 'score',
})|merge(
craft.entries({
search: {
query: activeQuery,
subRight: true,
},
order: 'score',
})
)|multisort(
['searchScore'],
[SORT_DESC]
) %}
{% if searchResults is not empty %}
{{ searchResults|length }} result{{ searchResults|length > 1 ? 's were' : ' was' }} found for "{{ activeQuery }}"
{% endif %}
{% if activeQuery and searchResults is empty %}
No results found for "{{ activeQuery }}".<br/>
Try adjusting your keywords and try again.
{% endif %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment