Discuss and resolve questions on Liquid, JavaScript, themes, sales channels, and site speed enhancements.
Hi there,
I would like to do filtering of my collections by metafields in liquid. How should I do it?
If I use the where in collection predefined fields, I can do it like this. {% assign collections = collections | where: 'description', 'promotion' %}
But while I am trying to do query on the 'group' metafield I have created, it simply not working.
{% assign collections = collections | where: 'metafields.custom.group', 'promotion' %}
I tried to use this solution (https://community.shopify.com/c/technical-q-a/filtering-collections-by-metafields-using-where/m-p/18...) and surf on the Internet and can get no useful information.
Please help Thanks
Solved! Go to the solution
This is an accepted solution.
I think the where in liquid won't be able to support a dot operator to retrieve nested array value.
The following is the definition of where in ruby. Unless array in Ruby support dot operator property value like 'metafields.custom.group', it cannot support for what I mentioned.
# @liquid_public_docs
# @liquid_type filter
# @liquid_category array
# @liquid_summary
# Filters an array to include only items with a specific property value.
# @liquid_description
# This requires you to provide both the property name and the associated value.
# @liquid_syntax array | where: string, string
# @liquid_return [array[untyped]]
def where(input, property, target_value = nil)
ary = InputIterator.new(input, context)
if ary.empty?
[]
elsif target_value.nil?
ary.select do |item|
item[property]
rescue TypeError
raise_property_error(property)
rescue NoMethodError
return nil unless item.respond_to?(:[])
raise
end
else
ary.select do |item|
item[property] == target_value
rescue TypeError
raise_property_error(property)
rescue NoMethodError
return nil unless item.respond_to?(:[])
raise
end
end
end
This is an accepted solution.
I think the where in liquid won't be able to support a dot operator to retrieve nested array value.
The following is the definition of where in ruby. Unless array in Ruby support dot operator property value like 'metafields.custom.group', it cannot support for what I mentioned.
# @liquid_public_docs
# @liquid_type filter
# @liquid_category array
# @liquid_summary
# Filters an array to include only items with a specific property value.
# @liquid_description
# This requires you to provide both the property name and the associated value.
# @liquid_syntax array | where: string, string
# @liquid_return [array[untyped]]
def where(input, property, target_value = nil)
ary = InputIterator.new(input, context)
if ary.empty?
[]
elsif target_value.nil?
ary.select do |item|
item[property]
rescue TypeError
raise_property_error(property)
rescue NoMethodError
return nil unless item.respond_to?(:[])
raise
end
else
ary.select do |item|
item[property] == target_value
rescue TypeError
raise_property_error(property)
rescue NoMethodError
return nil unless item.respond_to?(:[])
raise
end
end
end
Hey Community 👋 Did you know that March 15th is National Everything You Think Is W...
By JasonH Apr 1, 2025Discover how to increase the efficiency of commerce operations with Shopify Academy's l...
By Jacqui Mar 26, 2025Shopify and our financial partners regularly review and update verification requiremen...
By Jacqui Mar 14, 2025