Can you cross reference metaobjects in liquid?

Can you cross reference metaobjects in liquid?

SeanieB1983
Shopify Partner
20 1 7

Hi There 

I'm trying to build a store for a client and use metaobjects to create some 'website' content for them.

 

I have two metaobjects that I want to reference each other and display content from each other on their respective web pages.

 

The first one is for releases - this displays information for product releases

The other is videos which displays product videos.

 

What I need is to show videos on the releases page.  I've created a metaobject which allows me to apply the videos to the release;

SeanieB1983_0-1724597095132.png

 

I've tried {{ metaobjects.videos }} which returns the shopify g://ids but cannot find any way to access the metaobjects data - I've tried many different variations of the forloop to try and access it.

 

Does anyone know any way to access this data - would be odd to let us reference other metaobjects on a metaobject if we can't loop through and display the info on a page.

 

Sean

 

But I can't work out how to reference these in the liquid.

Replies 4 (4)

Sweans
Shopify Partner
397 76 118

Hi @SeanieB1983 ,

You're on the right track by using metaobjects, but accessing and looping through them requires a bit of structure in your Liquid code. To display the data from your video's metaobject on the release page, you need to:

1. Access the correct metaobject: If {{ metaobjects.videos }} is returning the g://ids, you’ll want to use those IDs to fetch the actual data from the metaobject.

2. Loop through the metaobjects: You can loop through the metaobject data by fetching the referenced metaobject entries based on the g://ids. Here's an example approach:

 

 

{% for video_id in metaobject_field_name %} 
  {% assign video = shop.metaobjects.videos[video_id] %}
  <div>
    <h2>{{ video.title }}</h2>
    <video controls>
      <source src="{{ video.video_url }}" type="video/mp4">
      Your browser does not support the video tag.
    </video>
  </div>
{% endfor %}

 

Replace "metaobject_field_name" with the actual field name for your videos in the "releases" metaobject. This code loops through the IDs, fetches each video metaobject by its ID, and displays the data on the page.

I hope this helps! If it does, please like it and mark it as a solution! 

If you need further assistance, feel free to reach out!

 

Regards,

Sweans

- Was my reply helpful? Please Like and Accept the Solution or let me know by Buying me coffee!
- Want to modify or custom changes on store Hire me.
- Feel free to contact me at info@sweans.com regarding any help.
- To know more about me check out www.sweans.com

SeanieB1983
Shopify Partner
20 1 7

Hi Sweans

Thanks for getting back to me, unfortunately, still struggling to return any values.

 

Here's the info for releases metafield that references the videos;

SeanieB1983_0-1724669701668.png

 

Here's the videos metafield

SeanieB1983_1-1724669730855.png

 

 

Have tried a few variations, am I misunderstandng;

<!-- One -->
{% for video_id in metaobject.videos %} 
  {% assign video = shop.metaobjects.videos[video_id] %}
  <div>
    <h2>{{ video.title }}</h2>
  </div>
{% endfor %}

<!-- Two -->
{% for video_id in metaobject.videos.values %} 
  {% assign video = shop.metaobjects.videos[video_id] %}
  <div>
    <h2>{{ video.title }}</h2>
  </div>
{% endfor %}

<!-- Three -->
{% for video_id in shop.metaobject.videos.values %} 
  {% assign video = shop.metaobjects.videos[video_id] %}
  <div>
    <h2>{{ video.title }}</h2>
  </div>
{% endfor %}

 

Any direction is greatly appreciated!

 

Sean

Sweans
Shopify Partner
397 76 118

Hi SeanieB1983,

 

Thank you for sharing the screenshots! It looks like the issue is related to how you're referencing the videos metafield data.

 

Here's an approach that should work:

{% for video_ref in metaobject.videos.values %}
{% assign video = shop.metaobjects.videos[video_ref] %}
<div>
<h2>{{ video.title }}</h2>
</div>
{% endfor %}

 


Explanation:

  • metaobject.videos.values: This will retrieve the list of video references stored in the releases.videos metafield.
  • shop.metaobjects.videos[video_ref]: This fetches the actual video metaobject using the reference ID or handle.


Using this approach should allow you to pull in the correct video title from your videos metaobject.

 

 

I hope this helps! If it does, please like it and mark it as a solution! 

 

If you need further assistance, feel free to reach out!

 

Regards,

Sweans

 

- Was my reply helpful? Please Like and Accept the Solution or let me know by Buying me coffee!
- Want to modify or custom changes on store Hire me.
- Feel free to contact me at info@sweans.com regarding any help.
- To know more about me check out www.sweans.com

SeanieB1983
Shopify Partner
20 1 7

Sorry, still not working.  Does it make a difference that its in the metaobject template in the theme editor;

SeanieB1983_0-1724741466372.png