Unlimited Metaobject Output: Solving the Loop Limit in Shopify Liquid
Unfortunately, Shopify has a limit when you try to access metaobjects directly in a loop. By default, any for
loop iterating through a metaobject definition's values, like shop.metaobjects.my_definition.values
, will only return a maximum of 50 items.
This becomes a problem when you need to display a complete list of items that exceeds this number. Common use cases include:
- Displaying all brand logos on a "Our Brands" page.
- Listing a full set of testimonials or reviews.
- Showing a comprehensive library of icons or feature badges.
If you have, for example, 60 brand logos stored as metaobjects, a standard loop will only show the first 50, making your list incomplete.
The 50-Item Limit in Practice
When you write a standard for
loop in Liquid to iterate through your metaobjects, you'll hit this cap. This implementation will only ever display up to 50 metaobjects, regardless of how many you've actually created.
{% comment %}
This loop is limited to the first 50 metaobjects.
{% endcomment %}
for tech_badge in shop.metaobjects.badge.values
<p>{{ tech_badge.name }}</p>
endfor
How to Avoid the Limit Using paginate
The solution is to wrap your loop in a paginate
tag. While often associated with products or articles, paginate
works perfectly for metaobjects and allows you to increase the number of items fetched per request.
You can set the pagination size up to a maximum of 250 for metaobjects, which is a fivefold increase over the default limit. For most scenarios, this is more than enough to retrieve all your entries at once.
{% comment %}
This loop can access up to 250 metaobjects.
{% endcomment %}
{%- paginate shop.metaobjects.badge.values by 250 -%}
{%- for tech_badge in shop.metaobjects.badge.values -%}
<p>{{ tech_badge.name }}</p>
{%- endfor -%}
{%- endpaginate -%}
By simply wrapping your for
loop with {% paginate ... by 250 %}
, you instruct Shopify to fetch a larger set of metaobjects, effectively bypassing the default 50-item restriction and allowing you to work with your complete dataset.
Insights from the Shopify Developer Community
The paginate
solution is a well-regarded "hack" or "undocumented feature" within the Shopify community. Here are some key insights and tips shared by developers who've tackled this issue:
- Acknowledge the "Hack": Be aware that this is a community-discovered workaround. The Liquid code editor might even show a warning about the pagination size, but it is safe to ignore as the code will execute correctly on the storefront.
- Variable Assignments: If you are assigning the metaobject values to a variable, ensure the
assign
declaration happens inside thepaginate
block to have access to the full list. - Avoid Caching Issues: One developer noted potential caching problems when placing the paginated loop inside a separate snippet that was then included on a page. To be safe, it's recommended to write the pagination logic directly in the section or template file where it's being used, rather than abstracting it to a snippet.
Are There Other Alternatives?
Before the paginate
workaround was widely known, some developers resorted to more complex solutions, such as:
- Backend Scripts & GraphQL: Writing a backend script (e.g., a cron job) to fetch all metaobjects via the Shopify's GraphQL API.
- External Storage: Storing the results in an external location, like a JSON file within the theme assets or a separate database.
- Manual Fetching: Using
fetch()
in JavaScript to read the JSON file and render the objects.
While this approach works, it is significantly more complex and brittle. For nearly all use cases, the paginate
method is the superior and recommended solution.
Check out how to merge variants from multiple products into one product page.