Reputation: 1528
I'm trying to get a list of related posts by tag using the Get helper in Ghost blog.
I tried to follow the cookbook in Ghost docs to get the related posts by tag, but it seems some of the syntax has changed slightly (I know it's a beta feature!).
But I got it working with this on my post.hbs (this is within the {{#post}}
block):
{{#get "posts" limit="4" include="tags" filter="tags.name:[Test]+posts.id:-{{id}}" as |related|}}
{{!-- do appropriate stuff --}}
{{/get}}
The obvious problem is the Test
tag is hardcoded here. Even though {{tags}}
outputs a list of tags for me, it doesn't work here:
App 22174 stderr: ERROR: Query Error: unexpected character in filter at char 11
App 22174 stderr: tags.name:[[object Object]]+pos
App 22174 stderr: -----------^
App 22174 stderr: Expecting 'NULL', 'TRUE', 'FALSE', 'NUMBER', 'LITERAL', 'STRING', got 'LBRACKET'
App 22174 stderr: Error parsing filter
App 22174 stderr: For more information on how to use filter, see http://api.ghost.org/docs/filter
Anyone have any ideas?
PS This is using version 0.11.7 (I just saw that 0.11.8 was recently released, but the changelog doesn't note anything related from what I can see).
Upvotes: 3
Views: 2651
Reputation: 661
I've also been having a problem with using the filter to get related posts by tags and I've finally solved it. So just in case you haven't, or for anyone else that may come across this...
I was having problems with parsing of the filter. This is my code now using the get
helper that is working for me (note: my sidebar is within my {{#post}}
block):
{{#get "posts" limit="3" filter="tags:[{{tags[*].slug}}]+id:-{{id}}" include="tags"}}
{{#foreach posts}}
<p>{{title}}</p>
{{/foreach}}
{{/get}}
One of my problems seemed to be the fact that some of my tags are 2 words, which meant I had to reference the slug of the tag (which is what tags[*].slug
is doing).
I also had one tag that had a single letter (Ghost automatically made the C# tag slug just c
) and the filter doesn't like that either. I edited the slug to be c-sharp
and now it's all working.
Upvotes: 6