Query all external tags and their attributed posts

Hello there!

I’d like to query all the External Tags and display its linked articles.

The code below does query all of the tags and outputs it correctly, but the posts are only general posts. I tried filtering with the rendered Tag Slug {{#get “posts” include=“tags” filter=“slug:{{slug}}”}} — but any sort of filter hides all the posts. Anyone has an example of another theme executing this, or can help? I try to aim for a similar effect like on the individual tags.hbs pages.

I only found this post, and the person seemingly successfully managed to do the same thing — so I was surprised it didn’t work out for me. Or is this not possible due to nested “get” helpers, as stated here?

Thank you for any sort of help!

{{#get "tags"}}
{{#foreach tags}}
<section>
	<div>
		/* Tag Name + Description */
		<h2>{{name}}</h4>
		<p>{{description}}</p>
	</div>

	/* Article List of Category */
    {{#get "posts" include="tags"}}
		<div>
		{{#foreach posts}}
			<article>
				<div>
					<p>{{tags limit="1"}}</p>
				</div>
				<h3><a href="{{url}}">{{title}}</a></h3>
			</article>
		{{/foreach}}
		</div>
	{{/get}}
</section>
{{/foreach}}
{{/get}}

Hey @christianeckert, I think you’re looking for the filter option. Take a look over here:

You’re pretty close already, you just need to filter your posts by the tag. The example in our docs is using primary_tag which I recommend to avoid duplication of posts.

Let us know how you get on!

Hello David,

Thank you for your answer. I am aware of the filter option as stated in my original post, and did try to get it working using the primary_tag filter.

Unfortunately the posts won’t be displayed as the {{name}} or {{slug}} attribute of the Tag does not get inserted in the {{#get “posts”}} query, e.g. {{#get “posts” filter="primary_tag:’{{name}}’}}

Any idea how to get and insert the tag name of the current {{#foreach Tags}} query object?

Thank you very much for any help!

Have you tried it without the single quotes? This works for me:

{{#get "tags"}}
  {{#foreach tags}}
    <section>
    	<div>
    		/* Tag Name + Description */
    		<h2>{{name}}</h4>
    		<p>{{description}}</p>
    	</div>

    	/* Article List of Category */
        {{#get "posts" include="tags" filter="primary_tag:{{slug}}"}}
    		<div>
    		{{#foreach posts}}
    			<article>
    				<div>
    					<p>{{tags limit="1"}}</p>
    				</div>
    				<h3><a href="{{url}}">{{title}}</a></h3>
    			</article>
    		{{/foreach}}
    		</div>
    	{{/get}}
    </section>
  {{/foreach}}
{{/get}}

Okay, I got it working! Thank you very much!
The problem though remains that the slug and the tag name need to be identical in this case, which is a little unfortunate. Is there something like {{#get “posts” include=“tags” filter=“primary_tag_id:{{id}}”}}?

I’m not sure what you mean? They don’t have to be identical. The outer tag loop has all the context of the tag, so the slug is just used as a reference between the tag context and the same tag within the post attributes.

Yes, I noticed that my slug started with a “0” that created the issue. So it all works fine & smoothly now. Thank you!

1 Like