Akola
June 10, 2025, 5:44am
1
Hi everyone,
I’m working with Shopify Metaobjects and trying to retrieve the number of images stored in a “List of images” field within one of my metaobjects.
My setup:
What I’m trying to do: I want to get the total number of images stored in this field.
My attempts so far:
{% assign my_metaobject = metaobjects.my_metaobject_definition.my-product-details %} {# Or however you are accessing your metaobject #}
{% assign image_count_attempt_1 = my_metaobject.images_gallery.count %}
{% assign image_count_attempt_2 = my_metaobject.images_gallery.size %}
<p>Image count (using .count): {{ image_count_attempt_1 }}</p> {# This outputs blank #}
<p>Image count (using .size): {{ image_count_attempt_2 }}</p> {# This also outputs blank #}
Try this pattern instead.
{% assign images = my_metaobject.images_gallery.value %}
{% if images %}
{% assign image_count = images | size %}
Image count: {{ image_count }}
{% else %}
No images found.
{% endif %}
Hey @Akola ,
I’ve run into this issue before, and I have a solution that should work perfectly for you.
The Problem: The .count and .size methods don’t work directly on metaobject list fields because Shopify returns them as special wrapper objects rather than standard arrays.
The Solution: You need to add .values to access the actual array of images. Here’s the corrected code:
{% assign my_metaobject = metaobjects.my_metaobject_definition.my-product-details %}
{% assign image_count = my_metaobject.images_gallery.values.size %}
<p>Image count: {{ image_count }}</p>
Why This Works:
The metaobject field itself is a wrapper object
.values gives you access to the actual array of items
Once you have the array via .values, you can use .size to get the count
Alternative Approach (if you prefer):
{% assign my_metaobject = metaobjects.my_metaobject_definition.my-product-details %}
{% assign images_array = my_metaobject.images_gallery.values %}
{% assign image_count = images_array.size %}
<p>Image count: {{ image_count }}</p>
Quick Debugging Tip: If you want to verify what’s in your field, you can use this:
<p>Image count: {{ my_metaobject.images_gallery.values.size }}</p>
<p>Images: {{ my_metaobject.images_gallery.values }}</p>
This same principle applies to all metaobject list fields (not just images), so remember to use .values whenever you’re working with lists in metaobjects.
Let me know if this solves your issue or if you run into any other problems!
Best regards,
Shubham | Untechnickle