Why is 'contains' not working with variables in product tags?

Topic summary

A developer encountered an issue where Liquid’s contains operator works with static values but fails when using variables to check product tags. Specifically, {% if prod.tags contains product.id %} doesn’t work, while {% if prod.tags contains '467455' %} does.

Root Cause Identified:
The problem stems from a type mismatch—product.id returns an integer, while tags are stored as strings. The contains operator requires both values to be the same data type.

Solution:
Convert the product ID to a string before comparison:

{% assign product_id = product.id | append: "" %}
{% if prod.tags contains product_id %}

This type conversion resolves the issue, allowing the variable-based tag matching to function correctly for displaying related products.

Summarized with AI on November 18. AI used: claude-sonnet-4-5-20250929.

{% if product.tags contains prod.title %} is not working. If I pass the static value with quote like- {% if product.tags contains ‘467455’ %} , it is working but pass the variable, not working. Please guide.

    {% paginate collections.all.products by 1000 %} {% for prod in collections.all.products %}

    {%- comment -%}here the issue if I use static value like=‘785643754’, it is working but when pass the variable. it is not working {%- endcomment -%}

    {% if prod.tags contains product.id %}

  • {{ prod.title }}

    {{ prod.price | money }} {%- if prod.compare_at_price > prod.price -%} {{ prod.compare_at_price | money }} {%- endif -%}

  • {% capture counter %}{{ counter }}*{% endcapture %} {% endif %}

    {% endfor %}
    {% endpaginate %}

Please help that how to fix. Its urgent

Unsolicited mentioned removed by Community Moderator

Hi @chandra_mmbo

I have read your code. And can’t find ‘{% if product.tags contains prod.title %}’.

Most close one is '{% if prod.tags contains product.id %} ', and the problem is ‘product.id’, you have not define ‘product’, so it should be ‘prod.id’

Hi @Kani

I have created a liquid file where I want to show related products by tags of current product. So I fetch all products by {% for prod in collections.all.products %}.

Now here, I am finding which products have current product id in tags(all products) {% if prod.tags contains product.id %}, but result not found.

{%- comment -%}{% if prod.tags contains product.id %} current product id = 8201838624999 {%- endif -%} {%- endcomment -%}

{% if prod.tags contains ‘8201838624999’ %} - when replace the product.id with static value of current product, its working.

Then I think you need try to print {{ product.id }}

{{ product.id }}

I print the product.id, it is displaying but in condition, not working.

While I am using {% if prod.type contains product.id %}. It is working. I am confuse

{{ prod.tags | json }} - [“8201838624999”,“Prints”]

I found the solution for this. Actually you are comparing a string with an integer. Both type should be the same when we use contains. Try the below code it will work

Here converted integer to string so contain work

{% assign product_id = product.id | append: "" %}

You can use like this

{% for prod in collections.all.products %}

{% assign product_id = product.id | append: "" %}
{% if prod.tags contains product_id %}
# Contain
{% else %}
# Not Contain
{% endif %}

{% endfor %}