Problem with filter by tags

Hello

Currently I’am trying to follow this tutorial to create a page displaying related post from using the tags of the current page.

But when I try to filter by "tags:[{{post.tags}}]" ghost gives me an error saying:

Error: Query Error: unexpected character in filter at char 7
(tags:[]+id:-)+type:post
-------^
Expecting 'NULL', 'TRUE', 'FALSE', 'NUMBER', 'LITERAL', 'STRING', got 'RBRACKET' 

I am trying to run this query from page.hbs but it is not working there or in post.hbs.

I also try to filter by "tags:[{{tags}}]" but it didn’t work either. In this case the output is:

Error: Query Error: unexpected character in filter at char 7
(tags:[[object Object],[obj

Thanks for any help.

Does your post have any tags? If it doesn’t, that seems to be why you’re running into issues.

You can work around this by surrounding the get block with an if statement:

{{#if post.tags.length}} {{!-- I'm not sure if it's post.tags or post.tags.length --}}
{{!-- Put your related posts block here --}}
{{/if}}

The page(post) has many tags related, but it seems the filter option do not accept an object as a parameter.

I figured out the issue! The get helper is designed to be run in the root context, not the post context. Moreover, the get helper has replacements - post.tags gets transformed into post.tags[*].slug, and expanded into the taglist via jsonquery

If that made no sense to you, here’s a breakdown:

{{!-- This will _NOT_ work --}}
{{#post}}
  {{#get "posts" limit="5" filter="tags:[{{tags}}]+id:-{{id}}" include="tags" as |related|}}
    {{!-- related posts go here --}}
  {{/get}}
{{/post}}

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

{{!-- And this will too! This is probably what you want to use --}}
{{#get "posts" limit="5" filter="tags:[{{tags[*].slug}}]+id:-{{id}}" include="tags" as |related|}}
  {{!-- related posts go here --}}
{{/get}}

Here’s the code block from the get helper that manages that logic:

That works perfect, thank you very much for the explanation.

Currently I’am trying to find a way to use the same filter but only for public tags, excluding internal ones. Is there some way to use the same filter to do that?

Try adding visibility: public to the filter

By default, internal tags are always included, use filter=visibility:"public" to limit the response directly or use the tags helper to handle filtering and outputting the response.

I’am currently in a {{#get “posts”}} helper with a filter tags:[{{tags[*].slug}}] so I think visibility: public wont work for the tags