Match recipes to products

So i have created product list metafield in my recipe blog posts so I can add which products from my store have been used in the recipe.

Now I want in each product to have a dropdown that will show the recipes its been used in

I added this code in main-product.liquid


this is what Its rendering


where am I going wrong?

What you’re getting from your metafield is a list of product references like

["gid://shopify/Product/4096776437814","gid://shopify/Product/4096747634742","gid://shopify/Product/4096776306742"]

However, your product.id is a number, like 4096776437814 and this is why you do not have a match.

The following should work, but this is still not a recommended way to access metafield.

{% if article.metafields.custom.ingredients contains product.id %}

Proper way would be

{% for p in article.metafields.custom.ingredients.value %}
    {% if p.id == product.id %}

or, I believe even like this

{% for p in article.metafields.custom.ingredients.value %}
    {% if p == product %}

or, probably faster (since no loop)

{% assign ingredient_ids = article.metafields.custom.ingredients.value | map: 'id' %}
{% if ingredient_ids contains product.id %}

Now, just a reminder that when you’re looping over all articles you may hit the pagination limit of 50.

1 Like

thank you, this worked


1 Like