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}}