What is the correct syntax to filter the blog post by the categories listed in the post_tags variable?
{% set post_tags = content.topic_list|map(‘name’)|join(‘,’) %}
{% set related_posts = blog_recent_posts(‘default’, 3, post_tags) %}
I don’t believe I have this part correct - (‘default’, 3, post_tags)
Thanks,
Terry McMillan
Hello @tmcmillan99
The syntax for filtering the blog post by the categories listed in the post_tags variable depends on how your HubSpot blog is set up and the type of filter you want to apply. Here are a few examples.
Filter by exact match on a single category
{% set related_posts = blog_recent_posts('default', 3, category=post_tags) %}
Filter by exact match on multiple categories
{% set post_tags_array = post_tags.split(',') %}
{% set related_posts = blog_recent_posts('default', 3, categories=post_tags_array) %}
Filter by partial match on multiple categories
{% set post_tags_array = post_tags.split(',') %}
{% set related_posts = blog_recent_posts('default', 3, categories_contains=post_tags_array) %}
In the first example, the category parameter is used to filter the blog posts by a single category. In the second example, the categories parameter is used to filter the blog posts by an array of categories. In the third example, the categories_contains parameter is used to filter the blog posts by an array of categories, matching partial matches.
@himanshurauthan
Thank you for the code examples. Unfortunately, I am unable to get them to work correctly. It still renders the 3 most recent blog posts instead of using the category from the viewed post. I did verify that the post_tags variable contains the correct post category (using {{ post_tags }} ). I even tried using a specific category to see if that would work - blog_recent_posts(‘default’, 3, category=‘Website Design’) but still received the same results.
Any other suggestions on what I can try?
I was able to get it working with the following code-
{% set related_posts = blog_recent_topic_posts(“default”, content.topic_list[0].slug, 3) %}