Order by custom metafield

Topic summary

A user needed to sort collections by a custom integer metafield but found that Shopify’s native sorting doesn’t support this functionality.

The Problem:

  • Attempted to use Liquid’s sort filter on collections with a custom metafield path
  • The sorting failed to apply despite the metafield being properly configured in admin

The Solution:

  • A workaround was provided using Liquid code that manually constructs a sortable string
  • The approach involves:
    • Looping through collections and concatenating metafield values with collection data
    • Splitting the concatenated string and applying sort_natural filter
    • Parsing the sorted results back into usable collection data

Outcome:
The workaround successfully resolved the issue, allowing collections to be ordered by the custom metafield value.

Summarized with AI on November 8. AI used: claude-sonnet-4-5-20250929.

I have created a custom metafield for collections with the type of Integer in admin. I am trying list the collection names and order them by the new custom metafield. Unfortunately the sorting does’t apply. Am I missing something?

{% assign sorted_collections = collections | sort: ‘metafields.custom.menu_sort.value’ %}
{% for collection in sorted_collections %}

{{ collection.title }} - {{collection.metafields.custom.menu_sort.value }}
{% endfor %}

Hi @kelliott ,

Shopify doesn’t support this one. You can refer code below:

{% assign sorted_collections = '' %}
{% for collection in collections %}
    {%  assign colTitle = collection.title  %}
    {%  assign colUrl = collection.url  %}
    {% assign sorted_collections = sorted_collections | append: collection.metafields.custom.menu_sort.value | append: "," | append: colTitle | append: "," | append: colUrl | append: ";"  %}
{% endfor %}
{%  assign  arrCollection = sorted_collections | split: ";"  | sort_natural %}
{% for col in arrCollection %}
    {%  assign collectionArr  = col | split: ","  %}
    
        {{ collectionArr[1] }}
    

{% endfor %}
1 Like

Thank you so much @EBOOST ! This worked perfectly!