Created
October 11, 2018 23:34
-
-
Save ben-rogerson/b1ba6889448dc1b0cceedf36c8e6804f to your computer and use it in GitHub Desktop.
How to include search queries into your Craft searches
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% 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