HUBL limiting dates

I’m working on a blog home landing page using a custom module with HUBL. The basic Hubl is working to pull the 3 most popular blog posts:

<div class=“popular-post”>
{% set pop_posts = blog_popular_posts(‘default’, 3) %} {% for pop_post in pop_posts %}
<p style=“font-size: 16px;”><img src=“{{ pop_post.featured_image }}” width=“150” style=“padding-right: 15px”>
<a href=“{{pop_post.url}}”>
{{ pop_post.name }}
</a></p>

Now I need to limit it to only include blogs published within the last year from the current date. I started with an if statement, but then found some filtering info, so I’ve been playing with:

{% set date = ‘null’ %}
{{date}}
{{date|minus_time(1, ‘years’) }}

But it’s not working, still pulling all time.

Any suggestions greatly appreciated!
</div>

Hey @mkarsten

I will tag a few of our experts to learn from them.

Hey @alyssamwilie @willsmith @stefen could you please share your knowledge with @mkarsten?

Thank you & Happy Holidays

Sharon
16x16_smiley-happy.png.png

Thanks for passing it on!

Hey @mkarsten

Could you try something like this?

<div class="popular-post">
 {% set pop_posts = blog_recent_posts('default', 3) %}
 {% set today = local_dt %} {# get and assign current date #}
 {% for pop_post in pop_posts %}

 {% set postDate = pop_post.publish_date %} {# get and assign post publish date per loop #}
 {% set yearDif = today|between_times(postDate, 'years') %} {# compare the date objects to the year - outputs an integer #}

 {% if yearDif <= 0 %} {# if the the yearDif is less than or equal to 0, print #}

 <p style="font-size: 16px;">
 <img src="{{ pop_post.featured_image }}" width="150" style="padding-right: 15px">
 <a href="{{pop_post.url}}">
 {{ pop_post.name }}
 </a>
 </p>

 {% endif %} 

 {% endfor %}
</div>

EDIT:

I just noticed that this could very well return less than 3 resualts. I will continue to explore the most effieicent option.

That might work tho… if they want to limit it to the past year, they might have to accept that there could be less than 3 posts.

What about increasing our dictionery and limiting the printed content that matches the year criteria?

{% set pop_posts = blog_recent_posts('default', 36) %} {# assuming you're publishing 3 posts a month - could be less to reduce load #}
{% set today = local_dt %} {# get and assign current date #}
{% set resCount = 0 %}

<div class="popular-post">
 {% for pop_post in pop_posts %}

 {% set postDate = pop_post.publish_date %} {# get and assign post publish date per loop #}
 {% set yearDif = today|between_times(postDate, 'years') %} {# compare the date objects to the year - outputs an integer #}

 {% if yearDif <= 0 %} {# if the the yearDif is less than or equal to 0, print #}
 {% if resCount < 3 %} {# if we've matched 3 we can ignore the rest #}
 {% set resCount = resCount + 1 %} {# resCount++ #}
 <p style="font-size: 16px;">
 <img src="{{ pop_post.featured_image }}" width="150" style="padding-right: 15px">
 <a href="{{pop_post.url}}">
 {{ pop_post.name }}
 </a>
 </p>
 {% endif %} 
 {% endif %} 

 {% endfor %}
</div>

Both worked great, but I went with the 2nd suggestion since there was concern about the first showing less than 3 posts. The styling changed a bit to match our main website on a different platform, but otherwise was a straight copy and paste. Appreciated the notes to understand what each step was doing. Thanks so much for the help!!!

I spoke too soon.:frowning: When I went to double check the blog posts the automation is pulling, it is bringing up the 3 most recent instead of the 3 most popular. When I change the first set to {% set pop_posts = blog_popular_posts(‘default’, 36) %} it then pulls all time instead of only from the last year. Any other suggestions?

I missed that the between_times will return the integer with the value of the difference. So on posts older than 1 year it returns -1 and not 1 like I assumed.

Fix:

Change the operator in the first for loop to be “>=” rather than “<=”.

{% set pop_posts = blog_popular_posts('default', 36) %} {# assuming you're publishing 3 posts a month - could be less to reduce load #}
{% set today = local_dt %} {# get and assign current date #}
{% set resCount = 0 %}

<div class="popular-post">
 {% for pop_post in pop_posts %}

 {% set postDate = pop_post.publish_date %} {# get and assign post publish date per loop #}
 {% set yearDif = today|between_times(postDate, 'years') %} {# compare the date objects to the year - outputs an integer #}

 {% if yearDif >= 0 %} {# if the the yearDif is less than or equal to 0, print #}
 {% if resCount < 3 %} {# if we've matched 3 we can ignore the rest #}
 {% set resCount = resCount + 1 %} {# resCount++ #}
 <p style="font-size: 16px;">
 <img src="{{ pop_post.featured_image }}" width="150" style="padding-right: 15px">
 <a href="{{pop_post.url}}">
 {{ pop_post.name }}
 </a>
 </p>
 {% endif %} 
 {% endif %} 

 {% endfor %}
</div>

Checked the stats and that did the trick! Thanks for the quick response.:slight_smile:

PS: The order looks correct.

Another note that just occured to me. I’m not sure what order the blog_popular_posts(‘default’, 36) request will return. It might be safe to assume that they’ll come in as most – least popular relatively. So they won’t be in chronological order if thats the case, but that if statement should still olny print values of a year or less.

If thats an issue we will have to look into reordering the pop_posts before running it through the printing logic.