Skip to content
Tutorials Center
My learning

Topics

Lesson 25 of 61 ยท Products, Collections and Variants

Collection Pages: Sorting, Filtering and Pagination

Build a Shopify collection page: loop products, paginate, add sort_by, storefront filters and facets, read filter URL parameters and keep pages fast.

A collection page is where shoppers browse. It shows a group of products, lets them sort by price or popularity, narrow the list with filters (in stock, brand, price range, color) and move through pages when there are more products than fit on one screen. Almost every store has one, and it is often the page with the most traffic after the home page.

Three Shopify features do the work: the paginate tag splits a long list into pages, the sort_by URL parameter changes the order, and storefront filtering (the collection.filters object plus filter.* URL parameters) narrows the list. All three are driven by the URL, which is what makes filtered pages shareable and bookmarkable. This lesson shows the Liquid you write and then a runnable stand-in that applies the same rules to plain data. It builds on Product, Collection, Cart and Page Templates.

Collection, filter and pagination objects exist only inside Shopify's Liquid, so the theme code below is read-only and written for current Online Store 2.0 themes. The live examples are JavaScript stand-ins that follow the same rules on mock products. Exact parameter names and limits can change, so confirm them in Shopify's current reference before shipping.

The collection object

On a collection template, Liquid gives you the collection object. The properties you use most:

Property Meaning
collection.title, collection.description name and rich description
collection.products the products on the current page, after sorting and filtering
collection.products_count how many products match the current filters
collection.all_products_count how many products are in the collection without filters
collection.filters the filter definitions to render (see below)
collection.sort_options the sort choices the merchant allows
collection.sort_by the sort currently applied
collection.default_sort_by the collection's default sort

A minimal product grid:

LIQUID
{% paginate collection.products by 12 %}
  <ul class="grid">
    {%- for product in collection.products -%}
      <li>{% render 'product-card', product: product %}</li>
    {%- else -%}
      <li>No products match these filters.</li>
    {%- endfor -%}
  </ul>

  {%- if paginate.pages > 1 -%}
    {{ paginate | default_pagination }}
  {%- endif -%}
{% endpaginate %}

Note the else branch of the for loop: it runs when the list is empty, which is exactly what happens when a filter combination matches nothing. Plan that empty state from the start.

Pagination

{% paginate collection.products by 12 %} takes the full list and gives you one page of it. Inside the block, a paginate object describes where you are:

Property Meaning
paginate.current_page page number, starting at 1
paginate.pages total pages
paginate.items total items across all pages
paginate.page_size items per page
paginate.previous, paginate.next link objects with url and title
paginate.parts the numbered links, for building your own pager

The page is chosen with the ?page= URL parameter, so /collections/shoes?page=2 is page two. The default_pagination filter renders ready-made links; for a custom pager, loop over paginate.parts and print links yourself. There is a maximum page size (Shopify documents 50 for paginate), so asking for more does not help; keep the number a multiple of your grid columns (12 works for 2, 3 and 4 columns).

Two performance notes. First, every product in the page costs Liquid time to render, so a smaller page size makes the first byte arrive sooner (see Liquid Performance). Second, pagination pages are separate URLs that search engines can crawl, so link to them with real <a href> links even if you later add "load more" with JavaScript.

flowchart LR
  U["URL: /collections/shoes<br/>?filter.v.availability=1<br/>&sort_by=price-ascending&page=2"] --> F["1. Apply filters"]
  F --> S["2. Sort"]
  S --> P["3. Paginate"]
  P --> R["collection.products<br/>(12 items for page 2)"]

The URL carries all state. Shopify filters first, sorts second and cuts the page last.

Sorting with sort_by

Sorting is a URL parameter: ?sort_by=price-ascending. The standard values are:

  • manual (the merchant's own order), best-selling
  • title-ascending, title-descending
  • price-ascending, price-descending
  • created-ascending, created-descending (oldest or newest)

Render a sort menu from collection.sort_options, so you only offer what the merchant enabled:

LIQUID
<form method="get" action="{{ collection.url }}">
  <label for="sort">Sort by</label>
  <select id="sort" name="sort_by" onchange="this.form.submit()">
    {%- for option in collection.sort_options -%}
      <option value="{{ option.value }}" {% if option.value == collection.sort_by %}selected{% endif %}>
        {{ option.name }}
      </option>
    {%- endfor -%}
  </select>
  <noscript><button>Apply</button></noscript>
</form>

Inline onchange is used here only to keep the example short; in a real theme attach the listener from your script file so a strict Content Security Policy does not block it. Keep the <noscript> button so the menu works without JavaScript.

Storefront filtering

Filtering (often called faceted navigation) lets shoppers combine criteria. Merchants configure which filters exist in the Shopify Search & Discovery app: product availability, price, vendor, product type, tags, and product options such as Color or Size, plus filters based on certain metafields. Your theme does not decide which filters exist; it renders whatever collection.filters contains.

Each entry in collection.filters is a filter object. The properties that matter:

  • filter.label: the heading ("Availability", "Brand", "Price")
  • filter.type: list, boolean or price_range
  • filter.param_name: the base name for the URL parameter, such as filter.v.price or filter.p.vendor
  • filter.values: for list and boolean filters, the choices
  • filter.min_value, filter.max_value, filter.range_max: for price ranges

Every value in filter.values has a label, a count (how many products would match), an active flag, a value to put in the form, and ready-made url_to_add and url_to_remove links.

The simplest approach is a <form method="get"> with a checkbox per value, named by the filter's parameter:

LIQUID
<form method="get" action="{{ collection.url }}" id="filters">
  {%- for filter in collection.filters -%}
    {%- case filter.type -%}
      {%- when 'price_range' -%}
        <fieldset>
          <legend>{{ filter.label }}</legend>
          <label>From
            <input type="number" name="{{ filter.min_value.param_name }}"
                   value="{{ filter.min_value.value }}" min="0"
                   max="{{ filter.range_max | money_without_currency | remove: ',' }}">
          </label>
          <label>To
            <input type="number" name="{{ filter.max_value.param_name }}"
                   value="{{ filter.max_value.value }}" min="0">
          </label>
        </fieldset>
      {%- else -%}
        <fieldset>
          <legend>{{ filter.label }}</legend>
          {%- for value in filter.values -%}
            <label>
              <input type="checkbox" name="{{ value.param_name }}" value="{{ value.value }}"
                     {% if value.active %}checked{% endif %}
                     {% if value.count == 0 and value.active == false %}disabled{% endif %}>
              {{ value.label }} ({{ value.count }})
            </label>
          {%- endfor -%}
        </fieldset>
    {%- endcase -%}
  {%- endfor -%}
  <input type="hidden" name="sort_by" value="{{ collection.sort_by }}">
  <button>Apply filters</button>
</form>

Checking a few boxes and pressing the button produces a URL such as ?filter.p.vendor=Northpeak&filter.v.availability=1&filter.v.price.lte=100. The same form works with JavaScript layered on top: intercept submit or change, build the same query string with FormData and URLSearchParams, fetch the page, and swap the product grid. That technique is the Section Rendering API.

The alternative is links: loop over values and print <a href="{{ value.url_to_add }}"> or url_to_remove for active ones. Links need no form, work for crawlers and are good for a small number of filters.

What the parameters look like

You will see these shapes in URLs (the names come from Shopify's filter system; the Search & Discovery app shows the exact set for a store):

  • filter.v.availability=1 in stock, =0 out of stock
  • filter.v.price.gte=20&filter.v.price.lte=80 a price range (store currency units, such as 20 for $20)
  • filter.p.vendor=Northpeak a product vendor, repeated for several: ...vendor=A&filter.p.vendor=B
  • filter.p.product_type=Jackets
  • filter.v.option.color=Blue a variant option value (the option name is part of the key)

Values in the same filter combine as OR (Blue or Green); different filters combine as AND (Blue-or-Green and in stock and under $80). Counts you display next to each value reflect the other filters that are active, so the numbers always say what you would get if you clicked.

SEO and filtered URLs

A site with six filters can generate thousands of URL combinations. Search engines do not need all of them. Common practice: keep the canonical URL pointing at the unfiltered collection, and avoid linking deep filter combinations in a way that crawlers follow endlessly. See SEO in Themes for canonical tags and robots rules.

Stand-in: filter, sort and paginate

The first stand-in applies the three steps from the diagram to a small catalog. It parses a real-looking query string with URLSearchParams, so you can see how a URL becomes a result list. This is not Shopify's engine, only the same rules written in JavaScript.

JS Stand-in: URL to filtered, sorted, paged list

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Notice the order of operations in the last two queries. Page 2 of the in-stock, price-descending list is computed after filtering and sorting, so the pages are stable. The final query shows the empty case your else branch must handle.

The second stand-in builds the filter.values list for the vendor filter: a label, a count, an active flag and the url_to_add or url_to_remove. The key idea is that each count is computed with every other filter applied but not the filter itself, so a shopper sees how many products they would get by also choosing that value.

JS Stand-in: facet counts and add/remove links

Press Run (or Ctrl + Enter) to see the console output here.

No output.

In a real theme you do not compute any of this: Shopify returns the finished objects and you only print them. Writing the stand-in is useful because it explains behaviors that surprise beginners, such as why a vendor you have not selected still shows a count that changes when you add a price filter.

Practical advice

  • Render the filter UI in a sidebar on desktop and a drawer on mobile, but from the same Liquid so both stay in sync.
  • Keep the active filters visible as removable chips (use each active value's url_to_remove) and offer a "Clear all" link.
  • Announce the new result count to assistive technology after a JavaScript update, for example in an aria-live="polite" region (see Accessibility in Themes).
  • Do not fetch product lists from the Ajax product endpoints to build your own filtering for a large collection; the built-in filters are faster and consistent with search.
  • For text search across the whole store, use search rather than filters; it has its own page and API in Search and Predictive Search.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. A shopper visits ?filter.p.vendor=Cobalt&filter.p.vendor=Ridgeline&filter.v.availability=1. How are these combined?

    Values of the same filter are alternatives (OR), while different filters narrow each other (AND).
  2. 2. Which tag splits collection.products into pages, and what picks the page?

    paginate wraps the product loop, and the page query parameter chooses which slice is returned.
  3. 3. Why does the theme not decide which filters exist?

    The theme renders whatever the merchant configured, so it must handle any filter type and any number of values.
  4. 4. What should the product loop do when a filter combination matches no products?

    An empty list is a normal result of filtering. The else branch of the for tag lets you explain it and offer a way to clear filters.

Summary

  • A collection page is URL-driven: filters, then sort, then pagination, all from query parameters.
  • {% paginate collection.products by N %} gives one page and a paginate object; the page parameter selects it.
  • Offer sorting from collection.sort_options and the sort_by parameter.
  • Render collection.filters generically (list, boolean, price range) with a form or with url_to_add and url_to_remove links.
  • Same-filter values are OR, different filters are AND; plan the empty state and watch filtered URLs for SEO.

Next, add store-wide discovery in Search and Predictive Search.