Skip to Content

Get HubSpot posts that contain all tags (Tag A AND Tag B AND Tag C)

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

HubSpot has a blog_recent_tag_posts function that can be used to fetch blog posts that have one of any of the tags listed. So something like: 

{% raw %}{{ blog_recent_tag_posts('default', ['development', 'hubspot'] ) }}

Would find any posts that have either the "development" tag OR the "hubspot" tag. But what if you want to find posts that have the "development" tag AND the "hubspot" tag?

The workaround

One solution that's worked for me is to get all posts with any of the tags, then check the list of tags on each post to make sure it contains all of them. 

The code looks like this:

{% set tagged_posts = blog_recent_tag_posts('default', ['tag-1','tag-2']) %}
{% set requiredTags = ['tag-1','tag-2'] %}
{% for tag_post in tagged_posts %}
  {% set matched_tags = 0 %}
  {% for tag in tag_post.tagList %}
    {% if tag.slug in requiredTags %}
      {% set matched_tags = matched_tags + 1 %}
    {% endif %}
    {% if loop.last %}
      {% if matched_tags == requiredTags|length %}
        // Print your post here
      {% endif %}
    {% endif %}
  {% endfor %}
{% endfor %}

 

An explanation

{% set tagged_posts = blog_recent_tag_posts('default', ['tag-1','tag-2']) %}

This gets all posts containing any of the tags. You can replace 'default' for a specific blog id and add as many tags into the tag list as needed, using the tag's slug.

 

{% set requiredTags = ['tag-1','tag-2'] %}

This sets the tags again, we'll need the variable to compare it with the blog post's tag list.

 

{% for tag_post in tagged_posts %}
  {% set matched_tags = 0 %}
  // Code
{% endfor %}

Loops through the tagged_posts variable and initialises a variable called matched_tags to count the number of tags on the post that match those in the requiredTags list.

 

{% for tag in tag_post.tagList %}
  {% if tag in requiredTags %}
    {% set matched_tags = matched_tags + 1 %}
  {% endif %}
  {% if loop.last %}
    {% if matched_tags == requiredTags|length %}
      // Print your post here
    {% endif %}
  {% endif %}
{% endfor %}

This part loops through the post's tag list. It checks to see if the tag is in the requiredTags list and, if it is, it increases the matched_tags variable.

In HubSpot, you can't update a variable within a loop and then call it outside of that loop, so a workaround to access that is to perform any actions that need that variable on the last loop. Under the loop.last condition, it checks to make sure the list of matched_tags is the same length as requiredTags (as it must have all tags) and, if it is, it outputs the post.

Where it says // Print your post here you can add in the code for your post, using any post variables (e.g. {{ tag_post.name }}code>).

Some limitations: 

Some limitations with this solution include:

←  Previous Article

Redirect a HubSpot form to a thank you page based on a form field

Next Article  →

Hiding HubSpot modules on mobile/desktop at template level

Contact

Get help from a HubSpot CMS Expert

  • Custom HubSpot themes and reusable and easy-to-use HubSpot templates and modules
  • Technical support and guidance on the HubSpot CMS