Other Related Videos module showing posts multiple times

Hi there,

I’m having an issue where a specific blog’s individual posts are showing posts multiple times in a custom module called Other Related Videos. Here is an example blog post where you can see posts repeated within the Other Related Videos module: Hardware Hipsters : FritsJurgens System Fx
These posts are created a single time in our backend, so I believe that the issue is coming from the module’s code. Below is the code in question:

<div id="related_posts_wrapper"><div id="related_posts_title"><h2>Other Recent Videos</h2></div><!--end related_posts_title-->{% set counter = 1 %} {% if content.topic_list %}<div id="portfoliolist">{% for topic in content.topic_list %}{% set related_posts = blog_recent_topic_posts('default', topic.slug, 4 ) %}{% for post in related_posts|sort(true, false, 'publish_date') %}{% if counter <= 3 %} {% if content.absolute_url != post.absolute_url %}{% set counter = counter + 1 %}<div class="portfolio"><div class="resource_guide"><div id="resource_guide_image"><a href="{{ post.absolute_url }}"><img src="{{ post.featured_image }}" loading="lazy" alt="{{ post.name }}"><span class="video_icon"><img src="https://resources.bridgeportworldwide.com/hubfs/Logos/logo-hardware-hipsters.png" alt="{{ post.name }}"></span></a></div><!--end resource_guide_image--><div id="resource_guide_text"><h3><a href="{{ post.absolute_url }}">{{ post.name }}</a></h3></div><!--end resource_guide_text--><div id="blog_card_date_topic"> <div class="blog_card_date"><p>{{ post.publish_date_localized }}</p></div><!--end blog_card_date--></div><!--end blog_card_date_topic--></div></div><!--end portfolio-->{% endif %}{% endif %}{% endfor %}{% endfor %}</div><!--end portfoliolist-->{% endif %}</div><!--end related_posts_wrapper-->

Is there anything here that stands out as causing this repeat issue?
Thank you,
MChe

There is a potential for duplication, I presume, based on the double for loop.

{% for topic in content.topic_list %}
{% set related_posts = blog_recent_topic_posts('default', topic.slug, 4 ) %}
{% for post in related_posts|sort(true, false, 'publish_date') %}

The first for loop is pulling the topics and then pulls all blog posts from said topic. The second for loop cycles through those posts. But if a blog post has multiple topics, it can pull the blog post again when it goes to the next topic.

Current Blog X = Topic 1, Topic 2, Topic 5

Blog A = Topic 1, Topic 2

Blog B = Topic 2

Blog C = Topic 1, Topic 3

Blog D = Topic 5

For Loop Run 1 on Topic 1 pulls A, C

For Loop Run 2 on Topic 2 pulls A, B

For Loop Run 3 on Topic 5 pulls D

You can see A is pulled twice.

Please note that blog_recent_topic_posts function is deprecated (Deprecated HubL functions - HubSpot docs).

Multiple ways to get around this. Easiest is to just pull all the blog posts in one go by passing in the slugs of all the relevant topics/tags using blog_recent_tag_posts (https://developers.hubspot.com/docs/reference/cms/hubl/functions#blog_recent_tag_posts).

So you might be able to do something like below without looping through the topics.

{% set related_posts = blog_recent_tag_posts('default', content.topic_list, 12 ) %}
{% for post in related_posts|sort(true, false, 'publish_date') %}

@MichaelMa is right about the double loop.
If you add the topic into the h3, you’ll see that the post is showing once for each topic set on the post

<h3><a href="{{ post.absolute_url }}">{{topic}} {{ post.name }}</a></h3>

Quick solution would be do only show related posts for the first topic

<div id="related_posts_wrapper">
<div id="related_posts_title">
<h2>Other Recent Videos</h2>
</div><!--end related_posts_title-->

{% set counter = 1 %} 
{% if content.topic_list %}

<div id="portfoliolist">
{% set topic = content.topic_list[0] %}
{% set related_posts = blog_recent_topic_posts('default', topic.slug, 4 ) %}
{% for post in related_posts|sort(true, false, 'publish_date') %}
{% if counter <= 3 %} 
{% if content.absolute_url != post.absolute_url %}
{% set counter = counter + 1 %}

<div class="portfolio">
<div class="resource_guide">
<div id="resource_guide_image">
<a href="{{ post.absolute_url }}"><img src="{{ post.featured_image }}" loading="lazy" alt="{{ post.name }}">
</a>
</div><!--end resource_guide_image-->

<div id="resource_guide_text">
<h3><a href="{{ post.absolute_url }}">{{ post.name }}</a></h3>
</div><!--end resource_guide_text-->

<div id="blog_card_date_topic"> 
<div class="blog_card_date">
<p>{{ post.publish_date_localized }}</p>
</div><!--end blog_card_date-->
</div><!--end blog_card_date_topic-->
</div>

</div><!--end portfolio-->

{% endif %}
{% endif %}
{% endfor %}
</div><!--end portfoliolist-->
{% endif %}
</div><!--end related_posts_wrapper-->

If you wanted to get a bit fancier you could make an array for the posts and only add the post if it’s not already in the array. I’d need to have a think about how to do that though.
EDIT:
Here’s how you’d do the fancier array method I mentioned:

<div id="related_posts_wrapper">
<div id="related_posts_title">
<h2>Other Recent Videos</h2>
</div><!--end related_posts_title-->

{% set counter = 1 %} 
{% if content.topic_list %}
{% set related_array = [] %}

{% for topic in content.topic_list %}
{% set related_posts = blog_recent_topic_posts('default', topic.slug, 4 ) %}
{% for post in related_posts|sort(true, false, 'publish_date') %}
{% unless post in related_array %}
{% do related_array.append( post ) %}
{% endunless %}
{% endfor %}
{% endfor %}

<div id="portfoliolist">
{% for post in related_array %}
{% if counter <= 3 %} 
{% if content.absolute_url != post.absolute_url %}
{% set counter = counter + 1 %}

<div class="portfolio">
<div class="resource_guide">
<div id="resource_guide_image">
<a href="{{ post.absolute_url }}"><img src="{{ post.featured_image }}" loading="lazy" alt="{{ post.name }}">
</a>
</div><!--end resource_guide_image-->

<div id="resource_guide_text">
<h3><a href="{{ post.absolute_url }}">{{topic}}{{ post.name }}</a></h3>
</div><!--end resource_guide_text-->

<div id="blog_card_date_topic"> 
<div class="blog_card_date">
<p>{{ post.publish_date_localized }}</p>
</div><!--end blog_card_date-->
</div><!--end blog_card_date_topic-->
</div>

</div><!--end portfolio-->

{% endif %}
{% endif %}
{% endfor %}
</div><!--end portfoliolist-->
{% endif %}
</div><!--end related_posts_wrapper-->

Barry Grennan

Freelance HubSpot CMS Developer

Website | Contact | LinkedIn

So, interestingly enough, I was able to remove the repeating posts by removing Tags from the posts Settings. I wasn’t expecting that, any idea why the Tags would be the reason to have caused this?

@MChe That would be expected. Exactly as @MichaelMa points out, you’re looping through the Topics (i.e. the Tags). The for each tag you’re looping through posts.


Barry Grennan

Freelance HubSpot CMS Developer

Website | Contact | LinkedIn