Show related posts from all associated tags


Hi there! I have been messing around with ghost on my local machine for a while now. I would describe myself as a guy that is not a coder/developer, but I do have more knowledge than the average person on websites, hosting, cms’s etc.

So now I am running into an issue that has been going on and on in my head, but I cannot find myself a solution after a long time of googling. So hopefully you guys will be able to help my mind get some rest on this matter :slight_smile:

Situation:
Imagine a box with 3x3 fields (9 in total). There are some routes to take on this box which are listed on a static page with several post lists (each with their own unique tag of the route).
For example there is route 1 - 4 - 7 but also a route 1 - 4 - 6 - 9.

Using {{#get “posts” filter=“tags:1469” limit=“3” order=“title asc”}} I am able to show all cards that use this tag.
Reference to screenshot on top

So all individual posts explain what to do to go from 1 to 4, 4 to 6 and 6 to 9 etc.

Now for example on the 1 to 4 page I would like related content to be posts: 4 to 7 (part of the 1 - 4 - 7 route), but also 4 to 6 and 6 to 9 (part of the 1 - 4 - 6 - 9 route).

So on the page “1 to 4” I am only getting “4 to 7” as related using this filter: filter=“tags:{{primary_tag.slug}}+id:-{{id}}”
Reference to screenshot on bottom

But I would also like to show post in that same list that have the tag 1469.

Hope my explanation is understandable! If not, I’ll try to formulate it again.

Take care of yourself and your surroundings - cheers, Mike

I don’t fully follow what you’re asking here - the numbers make it confusing.

But I think you’re asking for this:

https://ghost.org/tutorials/related-posts-block/

And specifically this:

{{#get "posts" limit="5" filter="tags:[{{post.tags}}]+id:-{{post.id}}" include="tags" as |related|}}
{{/get}}

Which is designed to fetch all posts with at least one tag the same as the current post, and behind the scenes Ghost automagically orders them by the number of matches. So a post with 2 matching tags appears before a post with one matching tag.

3 Likes

Hi Hannah! Thanks for your reply :smiley: I believe I tried that option before writing this topic, but when using the code it returns nothing in the browser (not even “No items found”, so I guess it is returning something I can’t see?).

This is the overall code as it was now, which only returns the cards that have the same primary tag, is there something I’m missing?

Edit: I tried pasting the full code including html, but that removed the html attributes… added a screenshot instead.

You might make better progress with using the code from the tutorial and working from there? In the tutorial it uses as |featured| within the #get helper, I believe this is needed to prevent identifier collisions with posts which may already exist on the page.

@Hannah I am looking for a tutorial on this also, but the link above goes to a 404. How To Build A Related Posts Block in Ghost - Tutorials

Can you help?

Thanks, Juergen

We’re currently working on reworking that tutorial as it was out of date.

However, a great resource for building a related posts block would be taking a look at how Casper does it.

Those lines of code provide everything you need to create the block. Feel free to ask here if you have questions!

1 Like

I’ve got a question! :sweat_smile:

Is there a way to exclude a tag (specifically an internal tag) from that related posts block? I’ve tried inserting -hash-removeme in a few places (with a comma before), but then the entire block doesn’t render.

You can try:

filter="id:-{{id}}+tag:{{primary_tag.slug}}+tag:-hash-remove"
3 Likes

Yes, that did it, thanks very much!

{{#get "posts" filter="tags:[{{post.tags}}]+id:-{{post.id}}" limit="12" as |more_posts|}}

Sorry for hijacking the post, but the script above is not working for me on the Casper theme.

It works if I remove the tags part:
{{#get "posts" filter="id:-{{post.id}}" limit="12" as |more_posts|}}

It works if I use a primary_tag filter:
{{#get "posts" filter="id:-{{post.id}}+primary_tag:{{post.primary_tag.slug}}" limit="12" as |more_posts|}}

Doesn’t seem to work when I use the tags collection, or when I try and combine multiple tag clauses.

Is it maybe because {{post.tags}} is returning the whole collection of tags, and not the individual tag slugs?

Thanks.

I worked this out here.