Related posts with latest fallback

I’ve implemented a simple related posts block. If there are other posts with a matching primary tag, I show related posts. If not, I show the latest posts.

{{#get "posts" limit="3" filter="primary_tag:{{primary_tag.slug}}+id:-{{id}}" include="authors,tags" as |related|}}
  {{#if related}}
    {{#foreach related}}
      {{> card}}
    {{#get "posts" limit="3" filter="id:-{{id}}" include="authors,tags" as |latest|}}
      {{#foreach latest}}
        {{> card}}

This works for related posts, but the latest posts only appear if I remove the filter. This isn’t ideal since I end up recommending the same post you’re currently reading.

I feel like it’s a simple solve, but I’m not sure what I’m doing wrong here. Appreciate any help!

That outer #get is messing up the inner #get’s ability to access id. You’d need something like …/id, but the filter won’t take that syntax. It gets messy fast.

I wrote a blog post about the topic here: A better related block for Ghost

Hey Cathy – I saw this article! Thanks for writing and responding.

I was trying to avoid nesting a bunch of partials since our “algorithm” is a bit simpler, but if that’s the only way to pass that “exclude” variable an extra context down… that makes total sense.