# Exclude current blog post from recent posts

**URL:** <https://community.hubspot.com/t/exclude-current-blog-post-from-recent-posts/13572>\
**Category:** CMS Development\
**Created:** [March 19, 2019, 11:49am UTC](https://community.hubspot.com/t/exclude-current-blog-post-from-recent-posts/13572 "2019-03-19T11:49:50Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![benji\_thecroc](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/benji_thecroc/32/31851_2.png) [@benji\_thecroc](https://community.hubspot.com/u/benji_thecroc)\
**Post date:** [March 19, 2019, 11:49am UTC](https://community.hubspot.com/t/exclude-current-blog-post-from-recent-posts/13572/1 "2019-03-19T11:49:50Z")

</div>

Hi,

I’m using this code snippet [here](https://designers.hubspot.com/en/docs/hubl/hubl-supported-functions#blog-recent-posts) to pull the three most recent blog posts on my blog post template. However I would like to adjust this functionality to exclude a blog post if it’s the one currently being viewed.

Is this acheivable?

Thanks,

[@benji\_thecroc](https://community.hubspot.com/t5/user/viewprofilepage/user-id/54367)

Benji

---

<div class="post-metadata">

**Author:** ![JasonRosa](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/jasonrosa/32/56551_2.png) [@JasonRosa](https://community.hubspot.com/u/JasonRosa)\
**Post date:** [March 19, 2019, 1:14pm UTC](https://community.hubspot.com/t/exclude-current-blog-post-from-recent-posts/13572/2 "2019-03-19T13:14:34Z")

</div>

[@benji\_thecroc](https://community.hubspot.com/t5/user/viewprofilepage/user-id/54367) I’d recommend using an HubL if statement to see if your current URL is equal to the URL in your loop. So something like this (if statement on line 3):

```auto
{% set rec_posts = blog_recent_posts('default', 4) %}
 {% for rec_post in rec_posts %}
 {% if content.absolute_url != rec_post.absolute_url %}
 <div class="post-title">{{ rec_post.name }}</div>
 {% endif %}
 {% endfor %}

```

---

<div class="post-metadata">

**Author:** ![benji\_thecroc](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/benji_thecroc/32/31851_2.png) [@benji\_thecroc](https://community.hubspot.com/u/benji_thecroc)\
**Post date:** [March 19, 2019, 2:54pm UTC](https://community.hubspot.com/t/exclude-current-blog-post-from-recent-posts/13572/3 "2019-03-19T14:54:03Z")

</div>

Hi @JasonRosa

This is helpful however this would result in only 3 recent posts being displayed instead of 4.

Ideally I’d want to exclude the current post from the ‘blog\_recent\_posts’ query and still pull in 4 blog posts rather than include current post in query but just not display it.

For example in a WordPress query within the argument array you can set something like -

```auto
<?php
$currentID = get_the_ID();
$my_query = new WP_Query( array('showposts' => '4', 'post__not_in' => array($currentID)));
while ( $my_query->have_posts() ) : $my_query->the_post(); ?>
<h2><a href="<?php the_permalink(); ?>"><?php the_title() ?></a></h2>
<?php the_content(); ?>
<?php endwhile; ?>

```

This picks up current post ID and excludes said post from the query results while retaining 4 posts in the loop.

---

<div class="post-metadata">

**Author:** ![JasonRosa](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/jasonrosa/32/56551_2.png) [@JasonRosa](https://community.hubspot.com/u/JasonRosa)\
**Post date:** [March 19, 2019, 3:13pm UTC](https://community.hubspot.com/t/exclude-current-blog-post-from-recent-posts/13572/4 "2019-03-19T15:13:00Z")

</div>

Hey, [@benji\_thecroc](https://community.hubspot.com/t5/user/viewprofilepage/user-id/54367) thank you for the response! That makes plenty of sense. Could you try something like this instead:

```auto
{% set rec_posts = blog_recent_posts('default', 5) %}
 {% set i = 0 %}
 {% for rec_post in rec_posts %}
 {% if content.absolute_url != rec_post.absolute_url %}
 {% if i > 4 %}
 <div class="post-title">{{ rec_post.name }}</div>
 {% set i = i + 1 %}
 {% endif %}
 {% endif %}
 {% endfor %}

```

This should pull in the 5 most recent posts (in case the most recent post is the one you’re currently viewing) and then show the first 4 that meet your criteria.

---

<div class="post-metadata">

**Author:** ![benji\_thecroc](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/benji_thecroc/32/31851_2.png) [@benji\_thecroc](https://community.hubspot.com/u/benji_thecroc)\
**Post date:** [March 19, 2019, 3:25pm UTC](https://community.hubspot.com/t/exclude-current-blog-post-from-recent-posts/13572/5 "2019-03-19T15:25:16Z")

</div>

That did the job, thank you for the solution!

---

<div class="post-metadata">

**Author:** ![JasonRosa](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/jasonrosa/32/56551_2.png) [@JasonRosa](https://community.hubspot.com/u/JasonRosa)\
**Post date:** [March 19, 2019, 3:26pm UTC](https://community.hubspot.com/t/exclude-current-blog-post-from-recent-posts/13572/6 "2019-03-19T15:26:01Z")

</div>

You’re very welcome!

---

<div class="post-metadata">

**Author:** ![jzilch](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/jzilch/32/2867_2.png) [@jzilch](https://community.hubspot.com/u/jzilch)\
**Post date:** [September 10, 2019, 3:03pm UTC](https://community.hubspot.com/t/exclude-current-blog-post-from-recent-posts/13572/7 "2019-09-10T15:03:46Z")

</div>

For anyone else looking for a solution down the line I have another example below.

Thanks Jason!

```auto
<h3>Latest posts</h3>
 <div class="cm-recent-blog-wrapper">
 {% set max_posts = 3 %}<!-- Set the max number of recent posts to be output to the page here -->
 {% set rec_posts = [] %}
 {% set rec_posts = rec_posts + blog_recent_posts('group.id', max_posts + 1) %}
 {% set i = 0 %}
 {% for post in rec_posts %}
 {% if content.absolute_url != post.absolute_url and i < max_posts %}
 <div class="recent-post-item">
 <a href="{{ post.absolute_url }}">
 {% if post.post_list_summary_featured_image %}
 <div class="recent-hs-featured-image-wrapper">
 <img src="{{ resize_image_url( post.post_list_summary_featured_image,767 ) }}" class="recent-hs-featured-image" alt="{{ post.featured_image_alt_text | escape }}">
 </div>
 {% endif %}
 <div class="recent-post-title">{{ post.name}}</div>
 </a>
 </div>
 {% set i = i + 1 %} 
 {% endif %}
 {% endfor %}
 </div>
</div>

```
