# HUBL limiting dates

**URL:** <https://community.hubspot.com/t/hubl-limiting-dates/17739>\
**Category:** CMS Development\
**Created:** [December 16, 2019, 9:44pm UTC](https://community.hubspot.com/t/hubl-limiting-dates/17739 "2019-12-16T21:44:54Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![mkarsten](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/mkarsten/32/70521_2.png) [@mkarsten](https://community.hubspot.com/u/mkarsten)\
**Post date:** [December 16, 2019, 9:44pm UTC](https://community.hubspot.com/t/hubl-limiting-dates/17739/1 "2019-12-16T21:44:54Z")

</div>

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\>

---

<div class="post-metadata">

**Author:** ![sharonlicari](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/sharonlicari/32/101479_2.png) [@sharonlicari](https://community.hubspot.com/u/sharonlicari)\
**Post date:** [December 20, 2019, 10:09am UTC](https://community.hubspot.com/t/hubl-limiting-dates/17739/2 "2019-12-20T10:09:57Z")

</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](https://d2qpr2jzcazej7.cloudfront.net/original/2X/2/22d5e40d253def47f5f4ea7f0e3e70be19010689.png)

---

<div class="post-metadata">

**Author:** ![mkarsten](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/mkarsten/32/70521_2.png) [@mkarsten](https://community.hubspot.com/u/mkarsten)\
**Post date:** [December 20, 2019, 2:36pm UTC](https://community.hubspot.com/t/hubl-limiting-dates/17739/3 "2019-12-20T14:36:18Z")

</div>

Thanks for passing it on!

---

<div class="post-metadata">

**Author:** ![Kevin-C](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/kevin-c/32/34570_2.png) [@Kevin-C](https://community.hubspot.com/u/Kevin-C)\
**Post date:** [December 31, 2019, 2:30pm UTC](https://community.hubspot.com/t/hubl-limiting-dates/17739/4 "2019-12-31T14:30:25Z")

</div>

Hey @mkarsten

Could you try something like this?

```auto
<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.

---

<div class="post-metadata">

**Author:** ![system](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/system/32/124_2.png) [@system](https://community.hubspot.com/u/system)\
**Post date:** [December 31, 2019, 3:19pm UTC](https://community.hubspot.com/t/hubl-limiting-dates/17739/5 "2019-12-31T15:19:56Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Kevin-C](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/kevin-c/32/34570_2.png) [@Kevin-C](https://community.hubspot.com/u/Kevin-C)\
**Post date:** [December 31, 2019, 3:40pm UTC](https://community.hubspot.com/t/hubl-limiting-dates/17739/6 "2019-12-31T15:40:28Z")

</div>

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

```auto
{% 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>

```

---

<div class="post-metadata">

**Author:** ![mkarsten](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/mkarsten/32/70521_2.png) [@mkarsten](https://community.hubspot.com/u/mkarsten)\
**Post date:** [January 2, 2020, 3:16pm UTC](https://community.hubspot.com/t/hubl-limiting-dates/17739/7 "2020-01-02T15:16:46Z")

</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!!!

---

<div class="post-metadata">

**Author:** ![mkarsten](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/mkarsten/32/70521_2.png) [@mkarsten](https://community.hubspot.com/u/mkarsten)\
**Post date:** [January 2, 2020, 3:28pm UTC](https://community.hubspot.com/t/hubl-limiting-dates/17739/8 "2020-01-02T15:28:23Z")

</div>

I spoke too soon.☹ 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?

---

<div class="post-metadata">

**Author:** ![Kevin-C](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/kevin-c/32/34570_2.png) [@Kevin-C](https://community.hubspot.com/u/Kevin-C)\
**Post date:** [January 2, 2020, 4:00pm UTC](https://community.hubspot.com/t/hubl-limiting-dates/17739/9 "2020-01-02T16:00:19Z")

</div>

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 “\<=”.

```auto
{% 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>

```

---

<div class="post-metadata">

**Author:** ![mkarsten](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/mkarsten/32/70521_2.png) [@mkarsten](https://community.hubspot.com/u/mkarsten)\
**Post date:** [January 2, 2020, 4:09pm UTC](https://community.hubspot.com/t/hubl-limiting-dates/17739/10 "2020-01-02T16:09:03Z")

</div>

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

PS: The order looks correct.

---

<div class="post-metadata">

**Author:** ![Kevin-C](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/kevin-c/32/34570_2.png) [@Kevin-C](https://community.hubspot.com/u/Kevin-C)\
**Post date:** [January 2, 2020, 4:09pm UTC](https://community.hubspot.com/t/hubl-limiting-dates/17739/11 "2020-01-02T16:09:09Z")

</div>

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.
