Jun 17, 20208:52 AM - edited Jun 17, 202010:02 AM
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 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!
{% 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.
{% 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.
Jun 17, 20208:52 AM - edited Jun 17, 202010:02 AM
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 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!
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?
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.
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.