search
Information about a storefront search query.
To learn about storefront search and how to include it in your theme, refer to Storefront search.
Properties
The filters that have been set up on the search page.
Only filters that are relevant to the current search results are returned. If the search results contain more than 1000 products, then the array will be empty.
TipTo learn about how to set up filters in the admin, visit the Shopify Help Center.
Returns
true
if a search was successfully performed. Returnsfalse
if not.- results
The search result items.
An item can be an
article
, apage
, or aproduct
.TipUse the paginate tag to choose how many results to show per page, up to a limit of 50.
ExampleSearch resultobject_type
Search results have an additional
property that returns the object type of the result.
{% for item in search.results %}<!-- Result {{ forloop.index }}--><h3>{{ item.title | link_to: item.url }}</h3>{% if item.object_type == 'article' -%}{%- comment -%}'item' is an articleAll article object properties can be accessed.{%- endcomment -%}{% if item.image -%}<div class="result-image"><a href="{{ item.url }}" title="{{ item.title | escape }}">{{ item | image_url: width: 100 | image_tag }}</a></div>{% endif %}{%- elsif item.object_type == 'page' -%}{%- comment -%}'item' is a page.All page object properties can be accessed.{%- endcomment -%}{%- else -%}{%- comment -%}'item' is a product.All product object properties can be accessed.{%- endcomment -%}{%- if item.featured_image -%}<div class="result-image"><a href="{{ item.url }}" title="{{ item.title | escape }}">{{ item.featured_image | image_url: width: 100 | image_tag }}</a></div>{% endif %}{%- endif -%}<span>{{ item.content | strip_html | truncatewords: 40 | highlight: search.terms }}</span>{% endfor %}{% for item in search.results %} <!-- Result {{ forloop.index }}--> <h3> {{ item.title | link_to: item.url }} </h3> {% if item.object_type == 'article' -%} {%- comment -%} 'item' is an article All article object properties can be accessed. {%- endcomment -%} {% if item.image -%} <div class="result-image"> <a href="{{ item.url }}" title="{{ item.title | escape }}"> {{ item | image_url: width: 100 | image_tag }} </a> </div> {% endif %} {%- elsif item.object_type == 'page' -%} {%- comment -%} 'item' is a page. All page object properties can be accessed. {%- endcomment -%} {%- else -%} {%- comment -%} 'item' is a product. All product object properties can be accessed. {%- endcomment -%} {%- if item.featured_image -%} <div class="result-image"> <a href="{{ item.url }}" title="{{ item.title | escape }}"> {{ item.featured_image | image_url: width: 100 | image_tag }} </a> </div> {% endif %} {%- endif -%} <span>{{ item.content | strip_html | truncatewords: 40 | highlight: search.terms }}</span> {% endfor %}
Output
- sort_
by The sort order of the search results. This is determined by the
URL parameter.
If there's no
URL parameter, then the value is
nil
.- sort_
options array of sort_option The available sorting options for the search results.
ExampleOutput the sort options{%- assign sort_by = search.sort_by | default: search.default_sort_by -%}<select>{%- for option in search.sort_options %}<optionvalue="{{ option.value }}"{%- if option.value == sort_by %}selected="selected"{%- endif %}>{{ option.name }}</option>{% endfor -%}</select>{%- assign sort_by = search.sort_by | default: search.default_sort_by -%} <select> {%- for option in search.sort_options %} <option value="{{ option.value }}" {%- if option.value == sort_by %} selected="selected" {%- endif %} > {{ option.name }} </option> {% endfor -%} </select>
Output
The entered search terms.
TipUse the
highlight
filter to highlight the search terms in search result content.
{
"default_sort_by": "relevance",
"filters": {},
"performed": true,
"results": [],
"results_count": 16,
"sort_by": "relevance",
"sort_options": [],
"terms": "potion",
"types": [
"article",
"page",
"product"
]
}