# related\_blog\_posts based on updated\_time

**URL:** <https://community.hubspot.com/t/related-blog-posts-based-on-updated-time/83449>\
**Category:** CMS Development\
**Tags:** starter\
**Created:** [June 15, 2023, 1:45am UTC](https://community.hubspot.com/t/related-blog-posts-based-on-updated-time/83449 "2023-06-15T01:45:49Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![a-a](https://avatars.discourse-cdn.com/v4/letter/a/8e7dd6/32.png) [@a-a](https://community.hubspot.com/u/a-a)\
**Post date:** [June 15, 2023, 1:45am UTC](https://community.hubspot.com/t/related-blog-posts-based-on-updated-time/83449/1 "2023-06-15T01:45:49Z")

</div>

How can I retrieve the latest three articles based on their update date and time?

{% macro related\_posts(post, count, total) %}  
{% if count == 1 %}  
\<ul\>  
{% endif %}

\<li\> // content // \</li\>

{% if count == total %}  
\</ul\>  
{% endif %}  
{% endmacro %}

{% related\_blog\_posts  
limit=3,  
no\_wrapper=True,  
post\_formatter=“related\_posts”  
%}

---

<div class="post-metadata">

**Author:** ![alyssamwilie](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/alyssamwilie/32/2072_2.png) [@alyssamwilie](https://community.hubspot.com/u/alyssamwilie)\
**Post date:** [June 15, 2023, 2:36am UTC](https://community.hubspot.com/t/related-blog-posts-based-on-updated-time/83449/2 "2023-06-15T02:36:26Z")

</div>

related\_blog\_posts is for getting posts related to the content of the current page. To get specific posts you would want to use the blog\_recent\_posts function instead. The function itself gets the most recently published, but you can use the |sort filter to instead sort it by updated date.  
A simple example for getting the most recently _updated_ posts:

```lia-code-sample
// set variable to hold posts, max of 250 is allowed
{% set posts = blog_recent_posts("default", 250) %}

// loop through posts, sorted by 'updated' attribute
{% for post in posts|sort(true, false, 'updated') %}
 // only show the first 3 posts
 {% if loop.index <= 3 %}
 // content you want to display
 <p>{{ post.name }} {{ format_datetime(post.updated, "MMM d, YYYY") }}</p>
 {% endif %}
{% endfor %}

```

---

<div class="post-metadata">

**Author:** ![a-a](https://avatars.discourse-cdn.com/v4/letter/a/8e7dd6/32.png) [@a-a](https://community.hubspot.com/u/a-a)\
**Post date:** [June 15, 2023, 3:25am UTC](https://community.hubspot.com/t/related-blog-posts-based-on-updated-time/83449/3 "2023-06-15T03:25:02Z")

</div>

Thank you!!

If I want to output the three latest posts on a post page, there is a possibility of overlap with the currently displayed post page using the given example.

Is there a way to avoid this overlap?

Or, can I sort using parameters or conditions with the following statement?

{% macro related\_posts(post, count, total) %}  
{% if count == 1 %}  
\<ul\>  
{% endif %}

\<li\> // content // \</li\>

{% if count == total %}  
\</ul\>  
{% endif %}  
{% endmacro %}

{% related\_blog\_posts  
limit=3,  
no\_wrapper=True,  
post\_formatter=“related\_posts”  
%}

---

<div class="post-metadata">

**Author:** ![alyssamwilie](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/alyssamwilie/32/2072_2.png) [@alyssamwilie](https://community.hubspot.com/u/alyssamwilie)\
**Post date:** [June 15, 2023, 12:29pm UTC](https://community.hubspot.com/t/related-blog-posts-based-on-updated-time/83449/4 "2023-06-15T12:29:02Z")

</div>

We can just add a check to see if the ID of the current content matches any of our three posts’ ID and, if so, skip it and show the next post instead.

```lia-code-sample
// macro for our displayed content
{% macro postFormatter(post) %}
 <p>{{ post.name }} {{ format_datetime(post.updated, "MMM d, YYYY") }}</p>
{% endmacro %}

{% set posts = blog_recent_posts("default", 250) %}
// variable that we'll update to let us know if current post exists in loop
{% set check = {currentPost: false} %}

{% for post in posts|sort(true, false, 'updated') %}
 {% if loop.index <= 3 %}
 // check if post ID matches current content's ID
 {% if post.id == content.id %}
 // if it does, update our check variable to true
 {% do check.update({'currentPost': true}) %}
 {% else %}
 // if it doesn't, display post content
 {{ postFormatter(post) }}
 {% endif %}
 {% endif %}

 // if our check variable is true, show the 4th post in the loop
 {% if loop.index == 4 && check.currentPost %}
 {{ postFormatter(post) }}
 {% endif %}
{% endfor %}

```

Sometimes the current post ID doesn’t come through in the page editor, so you may see the current post listed there, but when viewing a preview or published page the current post in the list will be replaced with a different post.
