CMS Development

aabrenes
Member

Alternative to blog_recent_tag_posts to get posts based on tags?

SOLVE

So we've got a customer who recently reached the max calls of 10 per page for the blog_recent_tag_posts hubl function - and they need to show more sections on the page. I was directed to look at the blog_tags function to pull in posts based on tags. They want to display posts based on a combination of tags as well (for example, return the latest 3 posts from XYZ Blog that have tags "Tag1", "Tag2" (or more).

 

I found the following code - but it uses that blog_recent_tag_posts function, but can't seem to get it to work with the blog_tags function....

 

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

0 Upvotes
1 Accepted solution
Indra
Solution
Guide | Diamond Partner
Guide | Diamond Partner

Alternative to blog_recent_tag_posts to get posts based on tags?

SOLVE

Hi @aabrenes,

 

You can use the blog_recent_posts function combining the selectattr filter.

It will look like this:

{# Set blog id #}
{% set blog_id = 'xxxxxxxxx' %}

{# Get all posts. Max limit is 200. Check https://developers.hubspot.com/docs/cms/hubl/functions#blog-recent-posts #}
{% set posts = blog_recent_posts(blog_id, 200) %}

{# Set a filter by tag by selectattr #}
{% for post in posts|selectattr('tagNames', 'containing', 'tag name 1') %}
    .. do something like print specific blog post information
{% endfor %}

{# Set a filter by tag by selectattr #}
{% for post in posts|selectattr('tagNames', 'containing', 'tag name 2') %}
    .. do something like print specific blog post information
{% endfor %}

For more efficient you can setup an array for your tag names and make it loop.


Bureau Vet

Did my post solve your question? Help the community by marking it as a solution

View solution in original post

3 Replies 3
rriveros
Member

Alternative to blog_recent_tag_posts to get posts based on tags?

SOLVE

Hi @Indra,

what we do if the list of posts available is longer than 200?

0 Upvotes
Indra
Guide | Diamond Partner
Guide | Diamond Partner

Alternative to blog_recent_tag_posts to get posts based on tags?

SOLVE

Hi @rriveros,

 

The 201 post will not be that important to be listed inside your custom listing page. If you want Google to index that post, make sure you have submitted your xml sitemap to Google Search Console and have other pages link to that 201th post for Google to index that blog.


Bureau Vet

Did my post solve your question? Help the community by marking it as a solution
0 Upvotes
Indra
Solution
Guide | Diamond Partner
Guide | Diamond Partner

Alternative to blog_recent_tag_posts to get posts based on tags?

SOLVE

Hi @aabrenes,

 

You can use the blog_recent_posts function combining the selectattr filter.

It will look like this:

{# Set blog id #}
{% set blog_id = 'xxxxxxxxx' %}

{# Get all posts. Max limit is 200. Check https://developers.hubspot.com/docs/cms/hubl/functions#blog-recent-posts #}
{% set posts = blog_recent_posts(blog_id, 200) %}

{# Set a filter by tag by selectattr #}
{% for post in posts|selectattr('tagNames', 'containing', 'tag name 1') %}
    .. do something like print specific blog post information
{% endfor %}

{# Set a filter by tag by selectattr #}
{% for post in posts|selectattr('tagNames', 'containing', 'tag name 2') %}
    .. do something like print specific blog post information
{% endfor %}

For more efficient you can setup an array for your tag names and make it loop.


Bureau Vet

Did my post solve your question? Help the community by marking it as a solution