Filtering tags page based on tag

I spent way too long working out the answer to this, so I thought I’d share.

How do I change the tags page (taxonomy) to exclude posts that are also tagged with something else.

I’m using a #archive tag to hide posts from lists (because they’re date-related), but I still want them to be accessible on the site.

I can’t use filters in the route as it’s a taxonomy.

The posts are displayed using a #foreach loop that uses an automatically generated collection.

Original tag.hbs

{{!< default}}

<div class="gh-outer">
<main class="site-main gh-inner">

    {{#tag}}
    <div class="term">
        <h1 class="term-name">{{name}}</h1>
        {{#if description}}
            <p class="term-description">{{description}}</p>
        {{/if}}
    </div>
    {{/tag}}

    <div class="post-feed gh-feed">
        {{#foreach posts order="featured desc, updated_at desc, created_at desc" }}
            {{> "loop"}}
        {{/foreach}}
    </div>

</main>
</div>

You need to put the #foreach loop inside a #get helper where you apply the filters you want.

In order to apply this to a tags page, you need to access the tag slug from the page, which is not available to the #foreach loop as the #tag helper is closed before the code gets there. So you need to move the {{/tag}} closing tag below the #get helper, too.

Final code:

{{!< default}}

<div class="gh-outer">
<main class="site-main gh-inner">

    {{#tag}}
    <div class="term">
        <h1 class="term-name">{{name}}</h1>
        {{#if description}}
            <p class="term-description">{{description}}</p>
        {{/if}}
    </div>

    <div class="post-feed gh-feed">
        {{#get "posts" limit="1000" filter="tag:{{slug}}+tag:-hash-archive" order="featured desc, updated_at desc, created_at desc" as |related|}}
            {{#if related}}
                {{#foreach related}}
                    {{> "loop"}}
                {{/foreach}}
            {{/if}}
        {{/get}}
    </div>
    {{/tag}}

</main>
</div>
2 Likes

I’ve been struggling with this too, approaching it through the routes file since I bought a brand new shiny theme, and don’t want to gut it.

There really should be a simple way to archive posts.

Edit: Still haven’t figured out how to do this.