Using #get to pull all posts (including paid members only) just like the API

Hi friends - I’m setting up a theme for Ghost which is going to use the membership bits and I’m very happy with what I’ve seen so far. One thing that’s missing, however, is full text search which is fine as I’ve been reading a lot of articles on how to do it.

Here’s the issue: when I use /ghost/api/v2/content/posts with axios, I get all my published posts, including the ones marked paid members only. This is what I want.

When I use {{#get posts}}, however, I _only get posts marked as “public” access. This seems like an inconsistency in the API and try as I might I can’t find the answer.

The reason I want to use the latter is that I have a contained amount of posts (< 50) for my site and, for speed reasons, I’d like to load up the Lunr search index with a JSON payload as opposed to an extra call after the page load.

Thanks!

{{#foreach visibility="all"}}
  <article>
    <h2><a href="{{url}}">{{title}}</a></h2>
  </article>
{{/foreach}}

Hi John and thank you :slight_smile: but the problem is with #get on a page without the posts context. I tried doing {{#get "posts" visibility="all"}} and the get operation doesn’t seem to return any posts unless they’re marked “public”. In other words: the solution you suggest here doesn’t work for my situation.

If I use the API all of the posts are returned, which does solve the problem from the client side, however I’d like to speed things up using a JSON payload.

Right, but {{#get "posts"}} is a posts context, and the only way to output posts once you’re in that context is with a foreach loop:

{{#get "posts"}}
    {{#foreach posts visibility="all"}}
        //thepost
    {{/foreach}}
{{/get}}

This will probably become implicit at the next major version break when it moves out of beta, atm it’s explicit for easier early development and to avoid accidents.

Perfect! Thank you - for some reason I had it in my head that the #get function would require the visibility filter in the same way you use filter. Anyway - thanks this is solved :).

Yeah I agree, the location of the param is a little confusing - hope to get that smoothed over with 4.0 :slight_smile: