CMS Development

JSheales
Member

related_blog_posts not pulling related posts

SOLVE

I have a blog post page template, using the related_blog_posts hubl tag at the bottom. However it is not actually pulling posts that are related. I have tried two methods.

 

This one is just taking the tags of the post, removing the year tag we have, as I only want to use the other tags, and passing that in to the hubl tag. Out of the 3 at most only 1 is actually related.

{% set tags = content.tag_list | join(',') %}

{% set tags = tags | cut('2025') %}
{% set tags = tags | cut('2024') %}
{% set tags = tags | cut('2023') %}

{% related_blog_posts limit=3, tags=tags, tag_boost="100" no_wrapper=True, post_formatter="recent_post" %}

 

I have also tried using hubl, to get the ids of 3 posts with the first tag, and pass the ID directly into the `blog_post_override` argument. This still does not display the selected posts.

{% set tags = content.tag_list | join(',') %}

{% set filteredTags = [] %}
{% for tag in tags %}
	{% set len = filteredTags | length %}
	{% if len < 10 and tag != "2025" and tag != "2024" and tag != "2023" %}
		{% set tag = tag | lower %}
		{% set tag = tag | replace(" ", "-") %}
		{% do filteredTags.append(tag) %}
	{% endif %}
{% endfor %}

{% set posts = blog_recent_tag_posts("default", filteredTags, 5, "AND") %}
{% set filteredPosts = [] %}
{% for post in posts %}
	{% if content.id != post.id %}
		{% do filteredPosts.append(post.id) %}
	{% endif %}
{% endfor %}
{% set filteredPosts = filteredPosts | join(",") %}

{% related_blog_posts limit=3, blog_post_override=filteredPosts, post_formatter="recent_post", no_wrapper=True %}

I have also tried this without the hubl, and just manually writing in the ids like "id1,id2,id3", still with no luck.

 

Any help or ideas would be greatly appreciated.

0 Upvotes
1 Accepted solution
JSheales
Solution
Member

related_blog_posts not pulling related posts

SOLVE

Edit: It was not displaying one pages with multiple tags as I was iterating through a string of tags not the array content.tag_list.

 

Changing that and making sure "blog_post_override" is passed in as "{{ stringvar }}" has worked.

View solution in original post

0 Upvotes
3 Replies 3
albertsg
Guide

related_blog_posts not pulling related posts

SOLVE

Hi @JSheales 👋

In your related_blog_posts call, I see the value of post_formatter="recent_post" but I don't see any macro with that name in your code. 

See the example in the boilerplate: https://github.com/HubSpot/cms-theme-boilerplate/blob/fbc829d502d53b75c2d7dadf160402f77cf2d863/src/t... 
As you can see, it's using the macro "related_posts" to format the output of the function related_blog_posts. 

Since you are missing it in your code (at least in the part you shared), maybe the issue is there. 

Here's another link with more information: https://developers.hubspot.com/docs/reference/cms/hubl/tags/related-blog-posts 

Let me know if that helps!



Did my answer help you? Mark it as a solution
0 Upvotes
JSheales
Member

related_blog_posts not pulling related posts

SOLVE

Hiya @albertsg,

 

Yhh thats not the issue, I didn't include the recent_post macro in the code as it was not relevant to the problem.

 

My posts are displaying as intended, its the actually finding related posts that is the problem. The module is fetching 3 seemingly random posts.

I did however change my second solution to pass in the filteredPosts as a string like:

 

{% related_blog_posts limit=3, blog_post_override="{{ filteredPosts }}", post_formatter="recent_post", no_wrapper=True %}

 

This has fixed it actually displaying the manually filteredPosts, but the "blog_recent_tag_posts" query does not always return related posts now, even after replacing "AND" with "OR" and manually checking that there are posts with that tag that should show.

 

Cheers,

James

0 Upvotes
JSheales
Solution
Member

related_blog_posts not pulling related posts

SOLVE

Edit: It was not displaying one pages with multiple tags as I was iterating through a string of tags not the array content.tag_list.

 

Changing that and making sure "blog_post_override" is passed in as "{{ stringvar }}" has worked.

0 Upvotes