Exclude current blog post from recent posts

SOLVE
Regular Contributor

Hi,

 

I'm using this code snippet here 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 

Benji

 

Reply
0 Upvotes
1 Accepted solution

Accepted Solutions
HubSpot Employee

Hey, @benji_thecroc thank you for the response! That makes plenty of sense. Could you try something like this instead: 

 

{% 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. 

5 Replies 5
HubSpot Employee

@benji_thecroc 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): 

 

{% 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 %}

 

Reply
0 Upvotes
Regular Contributor

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 -

 

<?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.

Reply
0 Upvotes
HubSpot Employee

Hey, @benji_thecroc thank you for the response! That makes plenty of sense. Could you try something like this instead: 

 

{% 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. 

Regular Contributor

That did the job, thank you for the solution!

Reply
0 Upvotes
Highlighted
HubSpot Employee

You're very welcome!

Reply
0 Upvotes