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!