Filter collections based on metafields

Filter collections based on metafields

paulrainstorm
Shopify Partner
18 0 2

Hello,

 

I'm building a Liquid page template to get collections with a single text line metafield Page Type "Shop By Make". However, it is returning as "No collections found."

 

Below is my code:

<div class="shop-by-make-container">
  <h1>Shop By Make</h1>

  {% assign filtered_collections = collections | where: "collection.metafields.custom.page_type.value", "Shop By Make" %}
  {% assign sorted_collections = filtered_collections | sort: "title-descending" %}

  {% if sorted_collections.size > 0 %}
    <ul class="shop-by-make-list">
      {% for collection in sorted_collections %}
        <li class="shop-by-make-item">
          <a href="{{ collection.url }}">
            {% if collection.metafields.custom.thumbnail_image %}
              <img src="{{ collection.metafields.custom.thumbnail_image | img_url: 'medium' }}" alt="{{ collection.title }}">
            {% else %}
              <img src="{{ collection.image | img_url: 'medium' }}" alt="{{ collection.title }}">
            {% endif %}
            <p>{{ collection.title }}</p>
          </a>
        </li>
      {% endfor %}
    </ul>
  {% else %}
    <p>No collections found.</p>
  {% endif %}
</div>

 

Found my reply helpful? Click Like to let me know!
Got your question answered? Mark it as an Accepted Solution.
Interested in working together? Contact Us Now!

Replies 2 (2)

Dotsquares
Shopify Partner
210 10 23

Hi @paulrainstorm , 

 

There is an issue in syntax.

 

Please try the below code:

<div class="shop-by-make-container">
  <h1>Shop By Make</h1>

  {% assign filtered_collections = collections | where: "metafields.custom.page_type.value", "Shop By Make" %}
  {% assign sorted_collections = filtered_collections | sort: "title" %} <!-- Sorting corrected -->

  {% if sorted_collections.size > 0 %}
    <ul class="shop-by-make-list">
      {% for collection in sorted_collections %}
        <li class="shop-by-make-item">
          <a href="{{ collection.url }}">
            {% if collection.metafields.custom.thumbnail_image %}
              <img src="{{ collection.metafields.custom.thumbnail_image | img_url: 'medium' }}" alt="{{ collection.title }}">
            {% else %}
              <img src="{{ collection.image | img_url: 'medium' }}" alt="{{ collection.title }}">
            {% endif %}
            <p>{{ collection.title }}</p>
          </a>
        </li>
      {% endfor %}
    </ul>
  {% else %}
    <p>No collections found.</p>
  {% endif %}
</div>

 

Hope this helps. Let me know if you need more help.

 

Regards,

Dotsquares Ltd


Problem Solved? ✔ Accept and Like solution to help future merchants.


Shopify Partner Directory | Trustpilot | Portfolio

DaisyVo
Shopify Partner
4338 482 568

HI @paulrainstorm 

 

Please try this code 

 

<div class="shop-by-make-container">
  <h1>Shop By Make</h1>

  {% assign filtered_collections = collections | where: "metafields.custom.page_type", "Shop By Make" %}
  {% assign sorted_collections = filtered_collections | sort: "title" | reverse %}

  {% if sorted_collections.size > 0 %}
    <ul class="shop-by-make-list">
      {% for collection in sorted_collections %}
        <li class="shop-by-make-item">
          <a href="{{ collection.url }}">
            {% if collection.metafields.custom.thumbnail_image != blank %}
              <img src="{{ collection.metafields.custom.thumbnail_image | img_url: 'medium' }}" alt="{{ collection.title }}">
            {% elsif collection.image %}
              <img src="{{ collection.image | img_url: 'medium' }}" alt="{{ collection.title }}">
            {% endif %}
            <p>{{ collection.title }}</p>
          </a>
        </li>
      {% endfor %}
    </ul>
  {% else %}
    <p>No collections found.</p>
  {% endif %}
</div>

 

I hope this helps

 

Best,

 

Daisy

Please let us know if our reply is helpful by giving it a Like or marking it as a Solution!

Avada SEO & Image Optimizer - The #1 SEO solution