How to get the count of items in a metafield that is a List

How to get the count of items in a metafield that is a List

ri31
Shopify Partner
40 2 18

FYI

Since I didn't find this from the documentation very clearly, here's an FYI on how to get a count of how many entries/items are in a metafield that is a list. The list could be a list of strings, a list of metaobjects, a list of files, etc. It will return a number, like "5".

 

Summary

 

  • Name the object
  • Name the metafield
  • Add .value.count to the end of the string

 

In a For loop that checks all of the objects and gives a count for each one:

Use the Liquid

{{ OBJECT.metafields.METAFIELD_NAMESPACE.METAFIELD__KEY.value.count }}

where the sections in all caps will vary. The metafield namespace is usually "custom".

 

Example:

{% for article in blogs.blog.articles %}

     {{ article.metafields.custom.custom_tags.value.count }}

{% endfor %}

 

 

To get the number of list items for a specific metaobject, from anywhere on the website

The handle of the individual object entry that you are targeting must be in square brackets and single quotes as shown. The formatting is similar for products or blog post articles, except instead of starting with shop meta objects you would start with collections.all.products... or blogs.BLOGNAME.articles...

 

{{ shop.metaobjects.METAOBJECT_DEFINITION_KEY['METAOBJECT-HANDLE'].METAFIELD_KEY.value.count }}

 

Example:

{{shop.metaobjects.famous_artists['elvis-presley'].published_songs.value.count}}

 

 

On the Template for a Metaobject Entry

For example, if you have a metaobject definition that is published as web pages, and you go to the visual builder and find the template for that, and add a custom code block, you would use this format.

{{ metaobject.METAFIELD_KEY.value.count}}

 

Example:

{{ metaobject.published_songs.value.count}}

If my post is helpful, hit Like to help others find a solution.
Replies 2 (2)

Havik
Excursionist
16 3 8

Hi Ri31,

Great knowledge sharing btw. This too has taken a lot of my time and much digging to get the answer to this, especially, the first time I dealt with Metaobjects. As you rightly pointed out, the docs was not verbose and newbie friendly in explaining how it works and to pull the value out of it.

Just to point out something interesting and I bet this will blow your mind if you didn't know prior to this, your point on "To get the number of list items for a specific metaobject, from anywhere on the website", where you use the metaobject handle to pinpoint the exact metaobject, there's an ongoing issue that if you have more than 20 metaobjects, you need to paginate and forloop it, even if you provide a metaobject handle 🙂

Thus, you still have a big performance overhead on your code due to this.

You can read more here on this thread,
https://community.shopify.com/c/technical-q-a/exceeded-maximum-number-of-unique-handles-for-metaobje...


Thanks again for this.

 

ri31
Shopify Partner
40 2 18

That's very good to know!

If my post is helpful, hit Like to help others find a solution.