Search code examples
javascripttemplateshandlebars.jsghost-blogghost

How to find all the posts with a specific tag in Ghost and iterate over them?


I'm currently working on a Ghost blog (Ghost is a Wordpress "successor" that is based on Node.js and other various packages/libraries on that platform), but I'm wondering how I might be able to grab all the posts that have a certain tag in Ghost/Handlebars.js.

The problem is that Ghost's contexts are usually encapsulated to the point that I can't extract a list of all the posts bearing a certain tag out of the API; it's apparently only possible to iterate through the posts from index.hbs, and other solutions are a bit hacker-y or involve more use of jQuery.

How might I be able to get a list or array of all the posts in Ghost so that I can filter them by tag and then iterate over them? I've even tried a {{#foreach posts}} and {{#has tag='WHATEVER'}} but this method doesn't seem to work out of the box. As a newbie to Ghost and Handlebars, I'm unsure of what to do.


Solution

  • In case anyone comes across this still, this is now possible. Here is how you can do it via the get helper:

    {{#get "posts" filter="tags:tagname"}}
        {{#foreach posts}}
             <p>{{title}}</p>
        {{/foreach}}
    {{/get}}
    
    {{#get "posts" filter="tags:[tag1, tag2]"}}
        {{#foreach posts}}
             <p>{{title}}</p>
        {{/foreach}}
    {{/get}}