How to retrieve article/blogpost if contains the main product in metafield product reference? PLEASE

Hello, I am trying to create a section for my product page basically exactly like the featured-blog.liquid section but instead it filters through to only display blogposts when that blogpost’s metafield product reference list contains the main product on the product page. I don’t know how to get it to work and render only those specific articles, here is my code please help!!!

{% assign related_articles = "" %}
{% for article in blogs.recipes.articles %}
  {% if article.metafields.product_info.related_products contains product.title %}
    {% assign related_articles = related_articles | append: article.id | append: "," %}
  {% endif %}
{% endfor %}
{% assign related_articles_ids = related_articles | split: "," %}

{%- liquid
  assign posts_displayed = related_articles_ids.size
  if section.settings.post_limit <= posts_displayed or section.settings.post_limit <= 8
    assign posts_exceed_limit = true
    assign posts_displayed = section.settings.post_limit
  endif
-%}

{% if related_articles %}
  <div class="blog color-{{ section.settings.color_scheme }} gradient{% if section.settings.heading == blank %} no-heading{% endif %}">
    <div class="page-width-desktop isolate{% if posts_displayed < 3 %} page-width-tablet{% endif %} section-{{ section.id }}-padding">
      {%- unless section.settings.heading == blank -%}
        <div class="title-wrapper-with-link{% if posts_displayed > 2 %} title-wrapper--self-padded-tablet-down{% else %} title-wrapper--self-padded-mobile{% endif %} title-wrapper--no-top-margin">
          <h2
            id="SectionHeading-{{ section.id }}"
            class="blog__title inline-richtext {{ section.settings.heading_size }}{% if settings.animations_reveal_on_scroll %} scroll-trigger animate--slide-in{% endif %}"
            {% if settings.animations_reveal_on_scroll %}
              data-cascade
            {% endif %}
          >
            {{ section.settings.heading }}
            {{ posts_displayed }}
          </h2>
  
          {%- if section.settings.blog != blank
            and section.settings.show_view_all
            and section.settings.post_limit < posts_displayed
          -%}
            <a
              href="{{ section.settings.blog.url }}"
              class="link underlined-link large-up-hide{% if settings.animations_reveal_on_scroll %} scroll-trigger animate--slide-in{% endif %}"
            >
              {{ 'sections.featured_blog.view_all' | t }}
            </a>
          {%- endif -%}
        </div>
      {%- endunless -%}
  
      <slider-component class="slider-mobile-gutter{% if settings.animations_reveal_on_scroll %} scroll-trigger animate--slide-in{% endif %}">
        <ul         
          id="Slider-{{ section.id }}"
          class="blog__posts articles-wrapper contains-card contains-card--article{% if settings.blog_card_style == 'standard' %} contains-card--standard{% endif %} grid grid--peek grid--2-col-tablet grid--{{ section.settings.columns_desktop }}-col-desktop slider {% if posts_displayed > 2 %}slider--tablet{% else %}slider--mobile{% endif %}"
          role="list"
        >
          {%- if posts_displayed > 0 -%}
            {%- for i in (1..posts_displayed) limit: section.settings.post_limit -%}
              {% assign article = related_articles_ids[i] %}
              <li
                id="Slide-{{ section.id }}-{{ forloop.index }}"
                class="blog__post grid__item article slider__slide slider__slide--full-width{% if settings.animations_reveal_on_scroll %} scroll-trigger animate--slide-in{% endif %}"
                {% if settings.animations_reveal_on_scroll %}
                  data-cascade
                  style="--animation-order: {{ forloop.index }};"
                {% endif %}
              >
                {% render 'article-card',
                  blog: section.settings.blog,
                  article: article,
                  media_aspect_ratio: 1.66,
                  show_image: section.settings.show_image,
                  show_date: section.settings.show_date,
                  show_author: section.settings.show_author,
                  show_excerpt: true
                %}
              </li>
            {%- endfor -%}

Thank you!!!

Hi @webdev100 ,

Please explain more about it? You want to display blog by product metafield on product page, or what do you want?
And send me the name of metafield, I will check it again

If you trace my previous posts i think it will be easier to understand what I am trying to achieve, but I will try to explain it to you properly and in detail here, Thank you so much in advance :slightly_smiling_face: I will reply again, let me just type it out LOL

So I have a blog called “recipes” in it contains various blogposts for various recipes. My store sells food products, so previously I created a metafield of product reference for the blogpost called related products ( article.metafields.product_info.related_products) and it allowed me to add various products to a list of products in that metafield to display as a custom block called related products under my article template sections. I had it render those products from the metafield in the block, but now I want to do the viceversa of the situation.

I want for each product page to contain a custom section similar to featured-blogs or the blog posts section you can add on shopify, but specifically it should filter through blogposts that do not contain the said hero product or main product i dont know how to refer it as. I was attempting to get the for loop in the top of the liquid code to retreive the article.id of each article’s metafield that contained the product object of the main product by running by each of the article in my ‘recipes’ blog’s array of articles.

{% assign related_articles = "" %}
{% for article in blogs.recipes.articles %}
  {% if article.metafields.product_info.related_products contains product.title %}
    {% assign related_articles = related_articles | append: article.id | append: "," %}
  {% endif %}
{% endfor %}
{% assign related_articles_ids = related_articles | split: "," %}

Then using the code of featured-blog.liquid, I want it basically to function the same as it so i just replaced some variables like posts_displayed and all that to match the size of my list of articles that are related (related_articles_id.size)

{%- liquid
  assign posts_displayed = related_articles_ids.size
  if section.settings.post_limit <= posts_displayed or section.settings.post_limit <= 8
    assign posts_exceed_limit = true
    assign posts_displayed = section.settings.post_limit
  endif
-%}

Then I wanted to render each of the related articles via their article id in the for loop and assigning the article to that specific article id from the list. Though through many tries and changes to the code it didn’t work because I really don’t understand much of the languages and functions various tags and whatnot, it wouldn’t render any articles at all. When the code runs all that shows up is the heading and the slider buttons at the bottom and the padding.

Please let me know if I should clarify on anything else, I would really like to figure this out!!! Thank you