CMS Development

aabrenes
Membre

Alternative to blog_recent_tag_posts to get posts based on tags?

Résolue

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 Votes
1 Solution acceptée
Indra
Solution
Guide | Partenaire solutions Elite
Guide | Partenaire solutions Elite

Alternative to blog_recent_tag_posts to get posts based on tags?

Résolue

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.


Vet Digital - The Growth Agency | HubSpot Solutions Partner Agency

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

Voir la solution dans l'envoi d'origine

3 Réponses
rriveros
Membre

Alternative to blog_recent_tag_posts to get posts based on tags?

Résolue

Hi @Indra,

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

0 Votes
Indra
Guide | Partenaire solutions Elite
Guide | Partenaire solutions Elite

Alternative to blog_recent_tag_posts to get posts based on tags?

Résolue

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.


Vet Digital - The Growth Agency | HubSpot Solutions Partner Agency

Did my post solve your question? Help the community by marking it as a solution
0 Votes
Indra
Solution
Guide | Partenaire solutions Elite
Guide | Partenaire solutions Elite

Alternative to blog_recent_tag_posts to get posts based on tags?

Résolue

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.


Vet Digital - The Growth Agency | HubSpot Solutions Partner Agency

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