Contact

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

    Difficulty Level: Advanced. This post assumes a strong understanding of HubSpot tools and technical knowledge. Insutructions are given as guidelines and may require additional customisation.

    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: 

    {{ 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:

    Last Updated: June 18, 2020

    Thank you! I appreciate the feedback 😊

    Sorry to hear that! 🙁

    I'd appreciate if you could take a moment to let me know how I can improve this article.

    hand-drawn-monitor-and-mouse

    Looking for a HubSpot website developer?

    Let me help you with website themes, templates, page setup and custom modules. 

    Get in touch

    Not using HubSpot yet?

    HubSpot offers a free CRM as well as Marketing, Sales and Services tools (with free options too!) and a powerful CMS. Learn more here.

    Note, as a HubSpot affiliate I will get a commission if you click on the link above and end up purchasing an upgraded version. No pressure though!

    Stephanie O'Gay Garcia

    Hi! 👋 I'm a freelance HubSpot Website Designer & Developer based in Toronto (Canada). Give me a shout if you have questions about this post or there's anything I can help you with.