Filter related posts by tags not working

I’m using Casper theme (v5.4.1) on Ghost(Pro).

I’m trying to create a related posts filter using the following script:

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

This doesn’t work - I don’t get any related posts.

I get related posts if I remove the tags part:
{{#get "posts" filter="id:-{{}}" as |more_posts|}}

…or if I change to use a primary_tag filter:
{{#get "posts" filter="id:-{{}}+primary_tag:{{post.primary_tag.slug}}" as |more_posts|}}

Doesn’t seem to work when I use the tags collection.

Am I doing something wrong?


post.tags won’t work for the filter.

Try tag:[{{tags.[*].slug}}]

Also, a good tool to help you debug here is running Ghost in development mode because it’d inform you if your filter is erroring out. Starting Ghost in dev mode is part of this tutorial:

Thanks, Ryan, I realised I was using the plural tags instead of tag, but that didn’t help.

tag:[{{tags.[*].slug}}] didn’t work.

tag:[{{post.tags.[*].slug}}] didn’t work.

tag:[{{post.tags}}] didn’t work.

Also, not sure I can enable debug mode in Ghost(Pro)?

Where are you calling this? Inside the {{#post}} context or outside of it?

Inside the {{post}} context, inside the {{#if @custom.show_recent_posts_footer}} condition.

But…I worked it out. The correct syntax to match on all tags, in order of the number of matches, is:

{{#get "posts" filter="id:-{{id}}+tags:[{{tags[*].slug}}]" as |more_posts|}}

There is no . between tags and [*].

Also, there are two syntaxes:

  1. Singular tag to match on just one tag:
    {{#get "posts" filter="id:-{{id}}+tag:{{primary_tag.slug}}" limit="3" as |more_posts|}}

  2. Plural tags to match one or more tags in a comma-separated list/array:
    {{#get "posts" filter="id:-{{id}}+tags:[{{primary_tag.slug}}]" limit="3" as |more_posts|}}

Both of the above work and achieve the same result because the list of tags in the second example is only given one tag to match.

To match on all the post’s tags you need +tags:[{{tags[*].slug}}].

Thanks for inspiring me to figure this out, @RyanF.

1 Like

Okay…I was wrong. This doesn’t appear to be working.

I have a page with tags 4 tags: night, adelaide, sa, beginner.

The first related post had tags: wildlife, river, wetlands, fog dam, mary river, darwin, nt, beginner.

That’s a 1/4 tag match.

There are other posts not in the related list with tags: night, glenelg, sa, beginner.

That’s a 3/4 tag match. Why isn’t that one in the list?