Loop through posts within a tag loop

Hello!

I am trying to get all the tags for my ghost.org site, loop through them and then show the 3 most recent posts.

I have read about problems with nested get helpers, but cannot see any way around not using them.

The best example I can give is; https://ease.ghost.io , Underneath “Featured articles”

Here is my code;

{{#get 'tags' limit='all' include="count.posts" order="count.posts desc" as |alltags|}}
  {{#foreach tags}}
    <a href='{{ url }}'>{{ name }}</a>
    <p>{{description}}</p>
    {{#get "posts" limit="3" filter="tag:{{tags.slug}}" include="tags,authors"}}
        <ul>
          {{#foreach posts}}
              <li>{{ title }}</li>
          {{/foreach}}
      </ul>
    {{/get}}
  {{/foreach}}
{{/get}}

Thanks, have a great day!

1 Like

You are close, a few adjustments are needed.

First if you are using the as property then you would have to use the same naming in the #foreach loop (but it can be done without that as well).

Second thing regarding the filter, you can either filter by primary_tag or by tag and since you are in tag context, you should be able to simply access the slug like this:

filter="tag:{{slug}}"

Another thing to consider if you are using the members feature, when you loop through your posts if you want to show member and paid posts, you should set the visibility parameter:

{{#foreach posts visibility="all"}}

In the end it should look like this:

{{#get "tags" limit="all" include="count.posts" order="count.posts desc" as |alltags|}}
  {{#foreach alltags}}
    <a href="{{url}}">{{name}}</a>
    <p>{{description}}</p>
    {{#get "posts" limit="3" filter="tag:{{slug}}" include="tags,authors"}}
        <ul>
          {{#foreach posts}}
              <li>{{title}}</li>
          {{/foreach}}
      </ul>
    {{/get}}
  {{/foreach}}
{{/get}}

I can’t test it right now but this should work.

Depending on your data it might be a good idea to limit the number of tags you are fetching initially.

3 Likes

Thank you very much for the reply,

It seems to be working for the first tag, but not for the rest.

I have added a

<p>{{ count.posts }}</p>

It outputs the number of posts on each tag, just doesn’t show the posts themselves via the for each loop.

If you filter by the primary_tag, then only the posts with that primary_tag will be shown under that tag.
This is useful if you want to make sure posts will not appear under multiple tags, otherwise you can simply use *tag in the filter.

1 Like

Tried that too, only outputs the first tags post.

I know each tag has posts as the ‘count.posts’ say so.

I feel like it’s possible to do this, but at the same time I keep reading problems with async and get helpers?

{{#get "posts" filter="tag:{{slug}}"}}
 <ul>
   {{#foreach posts}}
     <li>{{ title }}</li>
   {{/foreach}}
 </ul>
{{/get}}

For me it works locally, do you have member and paid posts? as I mentioned in that case you need visibility="all".

1 Like

Must of skimmed that bit,

Works perfectly now! Thank you so much for your help

2 Likes