# metafield

**URL:** <https://community.shopify.com/t/metafield/350191>\
**Category:** Shopify Discussion\
**Tags:** troubleshooting\
**Created:** [August 20, 2024, 12:25am UTC](https://community.shopify.com/t/metafield/350191 "2024-08-20T00:25:53Z")\
**Posts on this page:** 3\
**Page:** 1

## AI-generated summary

A user is working with two Shopify metafields of type "multiple values":

**Metafield 1:** Contains multiple product references (product1, product2, etc.)
**Metafield 2:** Contains multiple images (image1, image2, etc.)

**Goal:** Pair each image with its corresponding product by index position (image1 with product1, image2 with product2, etc.)

**Solution provided:** Use nested loops with `forloop.index` to match products and images by their position. The `map` filter can convert the image list to an indexed array for easier access.

**Remaining issue:** When a custom image is missing for a product, the code needs to fall back to the product's default featured image. This fallback functionality has not yet been implemented in the proposed solution.

<div class="post-metadata">

**Author:** ![Jack\_Khan\_Baba](https://avatars.discourse-cdn.com/v4/letter/j/e79b87/32.png) [@Jack\_Khan\_Baba](https://community.shopify.com/u/Jack_Khan_Baba)\
**Post date:** [August 20, 2024, 12:25am UTC](https://community.shopify.com/t/metafield/350191/1 "2024-08-20T00:25:53Z")

</div>

hi i have two metafileds with **type multiple values**  
**metafield no 1** select the multiple products like **product1,product2,product3,product4** etc  
then i show the title and link of that product.

**metafield no 2** select the multiple images like **image1,image2, image3,image4** etc

know i want that **image 1** is attached with **product 1** and **image 2** is attached with **product2** and so on.

how i can achieve this:  
here is the code:

{% for recommended\_product in product.metafields.custom.custom\_product.value %}

 

{{ recommended\_product.title }}

 

![]()

 

{% endfor %}

in the above code i want to change the **recommended\_product.featured\_image** with my custom images that are coming from metafields  
is there any solution?

---

<div class="post-metadata">

**Author:** ![tim\_tairli](https://yyz1.discourse-cdn.com/shopifyforum/user_avatar/community.shopify.com/tim_tairli/32/283567_2.png) [@tim\_tairli](https://community.shopify.com/u/tim_tairli)\
**Post date:** [August 20, 2024, 5:06am UTC](https://community.shopify.com/t/metafield/350191/2 "2024-08-20T05:06:00Z")

</div>

According to dox [https://shopify.dev/docs/api/liquid/objects#metafield-accessing-metafields-of-type-list](https://shopify.dev/docs/api/liquid/objects#metafield-accessing-metafields-of-type-list) you can only iterate over them, no direct access.

So it should be like:

```python

  {% for recommended_product in product.metafields.custom.custom_product.value %}
    
      {{ recommended_product.title }}

      {% assign product_index = forloop.index %}
      {% for image in product.metafields.custom.custom_image.value %}
        {% if forloop.index == product_index %}
          
        {% endif %}
      {% endfor %}
    
  {% endfor %}

```

Which does not look like the most effective/elegant.

So here is the trick which worked for me (because for images [_preview\_image_](https://shopify.dev/docs/api/liquid/objects/image#image-preview_image) points to themselves and _map_ kinda converts the list to a normal indexed array)

```python
{% assign images = product.metafields.custom.custom_image.value | map: "preview_image" %}

  {% for recommended_product in product.metafields.custom.custom_product.value %}
    
      {{ recommended_product.title }}

      
    
  {% endfor %}

```

---

<div class="post-metadata">

**Author:** ![Jack\_Khan\_Baba](https://avatars.discourse-cdn.com/v4/letter/j/e79b87/32.png) [@Jack\_Khan\_Baba](https://community.shopify.com/u/Jack_Khan_Baba)\
**Post date:** [September 10, 2024, 12:38am UTC](https://community.shopify.com/t/metafield/350191/3 "2024-09-10T00:38:04Z")

</div>

it works fine but if there is no custom image for some product it also need to pick

feature image from product
