Referencing metaobject values using variables

I’m trying to create a dynamic solution using metaobjects but struggling to get the syntax correct when referencing items of a specific metaobject definition using an assigned variable.

Let’s say I have 3 metaobjects “games_2024”, “games_2025”, “games_2026”. Each metaobject definition has the same structure: “description” and “game_date”. Creating many items in the appropriate metaobject will essentially create a list of games for each year.

I can easily reference each of the list of items by using the syntax:

{% for game in shop.metaobjects.games_2024.values %}

…do something custom like option input on product page so user can select game.

{% endfor %}

{% for game in shop.metaobjects.games_2025.values %}

…do something custom like option input on product page so user can select game.

{% endfor %}

But let’s now say that I want to make the solution dynamic by having a metafield definition on products that specifies the metaobject to use for pulling the game list. Field is a text input. Example: On the product the user would specify “games_2024” if they wanted all games for 2024.

In liquid code, I would like to iterate over the metaobject entries for the specified game list, but using the value from the product metafield instead of a hard coded value:

assign gamelist = “product.metafields.mynamespace.gamelist”

{% for game in shop.metaobjects.{{ gamelist }}.values %}

Unfortunately this code won’t compile.

I have also tried referencing using bracket notation but can’t get that to work either.

{% for game in shop.metaobjects.[‘{{gamelist}}’].values %}

I’m relatively new to liquid code so I hope I’m just not understanding the proper way to use the variable in the syntax. Thanks in advance.

Hi @ucfKingdom , You try the liquid below:

{% for game in product.metafields.mynamespace.gamelist.value %}

{{ game .description.value }}

{{ game .game_date.value }}

{% endfor %}

Yes…I have tried that syntax. It does compile ok but it does not access the items properly. I think using that syntax, it is looking for items with metaobject type “test”.

I think you should create a metafield of type Metaobject, then you reuse my liquid code above.

Thanks for the feedback on this Justin. Much appreciated. I actually tried that route first. I defined the metadata object and then created a metadata field of data type metaobject. The problem is that the user is forced to select one of the metaobject values on the product definition page. Example if there are 8 metadata items created of type “games_2024”, the user must select one from the list to associate with the product. I need to access all the items with metaobject “games_2024” so that I can present them in a drop-down on the product form that the end user can select from.

Hi @ucfKingdom ,

Thanks for the info, You can select “List off entries” when creating meta field to select multiple values.

1 Like

Got it! It worked great.

Thanks again for your help!

1 Like