How to combine posts from multiple HubSpot blogs in a custom module

Technical Difficulty: Advanced

Written by Stephanie O'Gay Garcia

First Published: April 6, 2022

Last Updated: May 27, 2022

⚠️ Note that this post hasn't been updated for at least a year and the information may be outdated, proceed with caution! (Last updated: May 27, 2022)

If a HubSpot website has multiple blogs, you may wish to combine posts from all of them to display on your home page.

This is how I've approached this problem in the past.

1/ Create your module's fields

The first thing I do is create some editable fields on the custom module but, if you're hard coding this information, you can skip this step:

  • Blogs: this is a "Blog" type field, and I switch on the repeater so that the user can select multiple
  • Tags (optional): this is a "Tags" type field, and I switch on the repeater as well so that the user can select multiple
  • Number of Posts: this is a "Number" field where the user can select how many posts will be displayed overall (note the limitations in the "Notes" section below). I like to add this to the "Styles" tab

This might look something like this for the Blogs and Tags fields (in both cases I've grouped the fields as well):

And like this for the Number of Posts field:

2/ Use HubL to combine blogs/tags

Under the HTML + HUBL field, the first thing I do is create an empty array (all_posts).

No Tags Field

If you haven't added a "Tags" field in Step 1 above, then you can use the following code to combine multiple blogs.

{# Create a variable to collect all posts #} {% set all_posts = [] %}

{# Loop through all posts selected in the blog field #} {% for item in module.blogs_to_display.blogs %}

{# Get posts for each blog #} {% set posts = blog_recent_posts(item, module.style.general.number_of_posts) %}

{# Append each post to the all_posts array #} {% for post in posts %} {% do all_posts.append(post) %} {% endfor %}

{% endfor %}

Using a Tags Field

If you're using a "Tags" field, you can use a condition to check if the user has selected any tags (if module.tags_to_display.tags|length > 0).

If so, the code within the condition is very similar to the above code to combine multiple blogs, but does so with the blog_recent_tag_posts function. This is the full code snippet:

{# Create a variable to collect all posts #} {% set all_posts = [] %}

{# Check if any tags were selected with the tags field #} {% if module.tags_to_display.tags|length > 0 %}

{# Tags Selected #} {# Create a variable to collect all tags #} {% set blog_tags = [] %}

{# Append each tag to the blog_tags array #} {% for item in module.tags_to_display.tags %} {% do blog_tags.append(item) %} {% endfor %}

{# Loop through all posts selected in the blog field #} {% for item in module.blogs_to_display.blogs %}

{# Get posts for each blog by tags #} {% set posts = blog_recent_tag_posts(item, blog_tags, module.style.general.number_of_posts) %}

{# Append each post to the all_posts array #} {% for post in posts %} {% do all_posts.append(post) %} {% endfor %}

{% endfor %}

{% else %}

{# Tags Not Selected #} {# Loop through all posts selected in the blog field #} {% for item in module.blogs_to_display.blogs %}

{# Get posts for each blog #} {% set posts = blog_recent_posts(item, module.style.general.number_of_posts) %}

{# Append each post to the all_posts array #} {% for post in posts %} {% do all_posts.append(post) %} {% endfor %}

{% endfor %}

{% endif %}

3/ Display your posts

Now you can loop through your all_posts array and display your posts.

{% for post in all_posts[:module.style.general.number_of_posts]|sort(true, false, 'publish_date') %}

{% endfor %}

Notes

Some items to note:

  • The blog_recent_posts HubL function allows a maximum of 200 posts to be retrieved and can only be called 10 times per page
  • The blog_recent_tag_posts HubL function allows a maximum of 200 posts to be retrieved and can only be called 10 times per page.