CMS Development

prasadcolumbus
Participant | Diamond Partner
Participant | Diamond Partner

How do I filter blogs with multiple Tags (i.e Tag 1 AND Tag 2 AND Tag 3)

SOLVE

Hello,

 

I am trying to filter multiple Tags on blogs section using below function.

 

{% set tag_posts = blog_recent_tag_posts('default', ['marketing', 'fun', 'inbound'], 3) %}

 

I need AND condition for 3 tags but working as OR condition

 

Can any one help how to put AND condition for 3 tags?

2 Accepted solutions
Stephanie-OG
Solution
Key Advisor

How do I filter blogs with multiple Tags (i.e Tag 1 AND Tag 2 AND Tag 3)

SOLVE

Hi there!

 

UPDATE: the below solution should work:

 

{% 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 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 %}

 

Add your tags to both the parameter in the blog_recent_tags_posts function and the requiredTags variable. Note that you should use the slug in the first (e.g. add in "-") and the name in the latter.

 

There's definitely an issue with just trying to match tag_post.tagList with the requiredTags variable using intersect (see below) so I looped through each item on the post's tag list and checked to see if they match what's in the requiredTags list instead.

 

Because you can't update a variable within a loop to be used outside of the loop, I've used the workaround of outputting the post details in the last iteration.

 

I hope that helps!

 

Previous reply (not working):

 

I think something like the below should work, although I can't actually seem to get it working... 

 

{% set tagged_posts = blog_recent_tag_posts('default', ['tag1','tag2']) %}
{% set requiredTags = ['tag1','tag2'] %}
{% for tag_post in tagged_posts %}
  {% if requiredTags|intersect(tag_post.tagList)|length == requiredTags|length %}
    // Print out your post here
  {% endif %}
{% endfor %}

What I'm trying to do is: 

  • Create a variable with the tags the post must have (requiredTags)
  • Loop through posts which have any of the tags using blog_recent_tag_posts
  • Loop through the list of tags on the post (tag_post.tagList)
  • Intersect both lists, if the number of items matches the length of the requiredTags list, print the post

I think the main issue is that, even if I print out requiredTags and tag_post.tagList and they look the same, the intersect isn't working. So I think there's something in how the tag_post.tagList is being accessed. Perhaps another loop is needed for the post's tag list.

 

I know it's not a full solution, but I think it's headed in the right direction and I have a few meetings to get to!

 


Stephanie O'Gay Garcia

HubSpot CMS Development

Website | Contact

 

If this helped, please mark it as the solution to your question, thanks!

View solution in original post

NMueller
Solution
Participant | Platinum Partner
Participant | Platinum Partner

How do I filter blogs with multiple Tags (i.e Tag 1 AND Tag 2 AND Tag 3)

SOLVE

I found a solution for this using intersect.

 

{% if tag_a_filter and tag_b_filter %}
  {% set tag_a_posts = blog_recent_tag_posts(group.id, tag_a_filter, 200) %}
  {% set tag_b_posts = blog_recent_tag_posts(group.id, tag_b_filter, 200) %}
  {% set filtered_posts = tag_a_posts|intersect(tag_b_posts) %}
{% endif %}

 

Then loop through the contents of "filtered_posts" as you would with blog_recent_tag_posts(). I like this better than using the method of filtering out posts within the for loop. It's a bit more elegant, and allows you to build pagination into the listing template, if needed.

View solution in original post

8 Replies 8
NMueller
Solution
Participant | Platinum Partner
Participant | Platinum Partner

How do I filter blogs with multiple Tags (i.e Tag 1 AND Tag 2 AND Tag 3)

SOLVE

I found a solution for this using intersect.

 

{% if tag_a_filter and tag_b_filter %}
  {% set tag_a_posts = blog_recent_tag_posts(group.id, tag_a_filter, 200) %}
  {% set tag_b_posts = blog_recent_tag_posts(group.id, tag_b_filter, 200) %}
  {% set filtered_posts = tag_a_posts|intersect(tag_b_posts) %}
{% endif %}

 

Then loop through the contents of "filtered_posts" as you would with blog_recent_tag_posts(). I like this better than using the method of filtering out posts within the for loop. It's a bit more elegant, and allows you to build pagination into the listing template, if needed.

Stephanie-OG
Solution
Key Advisor

How do I filter blogs with multiple Tags (i.e Tag 1 AND Tag 2 AND Tag 3)

SOLVE

Hi there!

 

UPDATE: the below solution should work:

 

{% 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 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 %}

 

Add your tags to both the parameter in the blog_recent_tags_posts function and the requiredTags variable. Note that you should use the slug in the first (e.g. add in "-") and the name in the latter.

 

There's definitely an issue with just trying to match tag_post.tagList with the requiredTags variable using intersect (see below) so I looped through each item on the post's tag list and checked to see if they match what's in the requiredTags list instead.

 

Because you can't update a variable within a loop to be used outside of the loop, I've used the workaround of outputting the post details in the last iteration.

 

I hope that helps!

 

Previous reply (not working):

 

I think something like the below should work, although I can't actually seem to get it working... 

 

{% set tagged_posts = blog_recent_tag_posts('default', ['tag1','tag2']) %}
{% set requiredTags = ['tag1','tag2'] %}
{% for tag_post in tagged_posts %}
  {% if requiredTags|intersect(tag_post.tagList)|length == requiredTags|length %}
    // Print out your post here
  {% endif %}
{% endfor %}

What I'm trying to do is: 

  • Create a variable with the tags the post must have (requiredTags)
  • Loop through posts which have any of the tags using blog_recent_tag_posts
  • Loop through the list of tags on the post (tag_post.tagList)
  • Intersect both lists, if the number of items matches the length of the requiredTags list, print the post

I think the main issue is that, even if I print out requiredTags and tag_post.tagList and they look the same, the intersect isn't working. So I think there's something in how the tag_post.tagList is being accessed. Perhaps another loop is needed for the post's tag list.

 

I know it's not a full solution, but I think it's headed in the right direction and I have a few meetings to get to!

 


Stephanie O'Gay Garcia

HubSpot CMS Development

Website | Contact

 

If this helped, please mark it as the solution to your question, thanks!

Sjardo
Top Contributor | Elite Partner
Top Contributor | Elite Partner

How do I filter blogs with multiple Tags (i.e Tag 1 AND Tag 2 AND Tag 3)

SOLVE

Hi,

 

I created a small update on the filters in this topic:
https://community.hubspot.com/t5/CMS-Development/Custom-Blog-Listing/m-p/445781#M23567

MarinaTakahasi
Member | Gold Partner
Member | Gold Partner

How do I filter blogs with multiple Tags (i.e Tag 1 AND Tag 2 AND Tag 3)

SOLVE

Hi Stephanie, how are you?
Im facing some issues when I try to bring dynamically the name of tag. If I write the name of tag it works fine but it will be fixed for all topic pages. Can you have some solution? 

Thank you.

0 Upvotes
prasadcolumbus
Participant | Diamond Partner
Participant | Diamond Partner

How do I filter blogs with multiple Tags (i.e Tag 1 AND Tag 2 AND Tag 3)

SOLVE

Thank you so much  @Stephanie-OG .It's working perfectly!

 One more help from youe end how to manage the pagenation?.Can you please share me the code as well.

 

0 Upvotes
Stephanie-OG
Key Advisor

How do I filter blogs with multiple Tags (i.e Tag 1 AND Tag 2 AND Tag 3)

SOLVE

No problem @prasadcolumbus , I'm glad that helped!

 

Unfortunately, you can't create real pagination using the blog_recent_tag_posts function. However, you could fake it using JavaScript. It's not ideal as it still technically loads all the elements on the page and then hides them, but if you're not loading too many items it could be an option.

 


Stephanie O'Gay Garcia

Freelance HubSpot CMS Developer

Website | Contact

 

0 Upvotes
prasadcolumbus
Participant | Diamond Partner
Participant | Diamond Partner

How do I filter blogs with multiple Tags (i.e Tag 1 AND Tag 2 AND Tag 3)

SOLVE

But our requirment is real pagination and multiple Tags (i.e Tag 1 AND Tag 2 AND Tag 3).Is their any outher solution @Stephanie-OG ?

0 Upvotes
Stephanie-OG
Key Advisor

How do I filter blogs with multiple Tags (i.e Tag 1 AND Tag 2 AND Tag 3)

SOLVE

As far as I know, you cannot pagination through any of HubSpot's functions to get posts (e.g. blog_recent_tag_posts), they're meant more to showcase blog posts on other parts of your website.

 

You can only paginate through the contents loop on a blog template, where you should be able to use the same code as above but in the contents loop instead of the tagged_posts loop.

 

Another option might be an RSS feed.

 


Stephanie O'Gay Garcia

Freelance HubSpot CMS Developer

Website | Contact

0 Upvotes