Order by custom metafield

Solved

Order by custom metafield

kelliott
Shopify Partner
2 0 0

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 %}
  <div class="order-form__collections-item">
      {{ collection.title }} - {{collection.metafields.custom.menu_sort.value }}
</div>
{% endfor %}
Accepted Solution (1)

EBOOST
Shopify Partner
1295 327 391

This is an accepted solution.

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: ","  %}
    <div class="order-form__collections-item">
        <a href="{{ collectionArr | last }}">{{ collectionArr[1] }}</a>
    </div>
{% endfor %}
- Hope can help. If you find my reply helpful, please hit Like and Mark as Solution
- Need a Shopify developer? Contact email: eboost10@gmail.com
- Visit our site: https://www.eboosttech.net to view and download shopify themes and magento2 extensions free
- ❤❤DONATE ❤❤Coffee tips

View solution in original post

Replies 2 (2)

EBOOST
Shopify Partner
1295 327 391

This is an accepted solution.

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: ","  %}
    <div class="order-form__collections-item">
        <a href="{{ collectionArr | last }}">{{ collectionArr[1] }}</a>
    </div>
{% endfor %}
- Hope can help. If you find my reply helpful, please hit Like and Mark as Solution
- Need a Shopify developer? Contact email: eboost10@gmail.com
- Visit our site: https://www.eboosttech.net to view and download shopify themes and magento2 extensions free
- ❤❤DONATE ❤❤Coffee tips
kelliott
Shopify Partner
2 0 0

Thank you so much @EBOOST ! This worked perfectly!