Filter last (most recent) post from get/for results

I’m trying to figure out a way to use filters to remove the last post from a certain tag from rendering. I figured out a way to remove it from a single index page, but now I also need to remove that from tag archives and related posts.

Any way to do that with filters? Thank you

Any help anyone?

One last try to get some help here: essentially what I’m trying to do is make that post disappear, even though it is set to public, from the site.

  • last post in the loop
  • remove from related posts
  • remove from next/previous post footer links
  • remove from the tag archive
  • remove from the author archive.

Any pointers? Thank you

If you want to avoid rendering the last post in a loop you can take a look at the available data helpers in the {{foreach}} helper:

Essentially in the {{foreach}} you can check:

{{#foreach posts}}
  {{#if @last}}
    <!-- do nothing? -->
  {{else}}
    <!-- your post card here -->
  {{/if}}
{{/foreach}}

Thank you so much for your suggestion!

So in the loop I’ve helped myself with the {{#if @first}} element, but the problem there is that it seems to break with pagination and removes that post on every page /n - any ideas?

Is there a similar helper as well for the tag helpers such as the related posts widget, the next/older posts?

{{#next_post in="primary_tag"}}
    {{#has tag=“x”}}
        {{> "post-card-rel-perf"}}
    {{else has tag="tips"}}
        {{> "post-card-rel-tip"}}
    {{else has tag="blog"}}
        {{> "post-card"}}
    {{else has tag="getting-started"}}
        {{> "post-card"}}
    {{else}}
        {{> "post-card"}}
    {{/has}}
{{/next_post}}
{{#if primary_tag}}
{{#get "posts" filter="tags:{{primary_tag.slug}}+id:-{{id}}" limit="3" as |related_posts|}}
    {{#if related_posts}}
    <article class="read-next-card">
        <header class="read-next-card-header">
            {{#../primary_tag}}
            <h3><span>More in</span> <a href="{{url}}">{{name}}</a></h3>
            {{/../primary_tag}}
        </header>
        <div class="read-next-card-content">
            <ul>
                {{#foreach related_posts}}
                <li>
                    <h4><a href="{{url}}">{{title}}</a></h4>
                    <div class="read-next-card-meta">
                        <p><time datetime="{{date format="YYYY-MM-DD"}}">{{date format="D MMM YYYY"}}</time> –
                            {{reading_time}}</p>
                    </div>
                </li>
                {{/foreach}}
            </ul>
        </div>
        <footer class="read-next-card-footer">
            <a href="{{#../primary_tag}}{{url}}{{/../primary_tag}}">{{plural meta.pagination.total empty='No posts' singular='% post' plural='See all % posts'}}
                →</a>
        </footer>
    </article>
    {{/if}}
{{/get}}
{{/if}}

Well, yes, this would apply your code whenever the {{foreach}} is used.
If you want this only on the home page for example then you can do an additional check for context ( {{#is "home"}})

Related posts use the foreach, so you can do the same thing there as well… You can check the docs for any context you are interested in.

1 Like

Right, but this helper isn’t inside a foreach

{{#next_post in="primary_tag"}}
    {{#has tag=“x”}}
        {{> "post-card-rel-perf"}}
    {{else has tag="tips"}}
        {{> "post-card-rel-tip"}}
    {{else has tag="blog"}}
        {{> "post-card"}}
    {{else has tag="getting-started"}}
        {{> "post-card"}}
    {{else}}
        {{> "post-card"}}
    {{/has}}
{{/next_post}}

is there a way to exclude the latest post inside that category from showing, if it is indeed on the second to last post?

Good point about the is home need to double-check if it works on a custom index page. Thanks

Edit: this might actually be even better: {{^is "paged"}} for that :)

You could theoretically fetch the latest post and check if next_post and the latest post have the same id… but it’s a bit overengineered, maybe there is a simpler solution…

What I don’t get is why do you want to hide the latest post from everywhere? Is this a specific post, if yes, then maybe simpler to exclude it by the id, or slug.

1 Like

I have a special kind of requirement that basically doesn’t let users see the newest post (it’s public, but still paid access). Unfortunately this was the simplest solution without having to constantly manually mark one post to private and the other one to public.