Collection: filtering on tags not working

Hi,

I have a site which I am trying to add multi-language support using collections and filtering.

I followed the instructions on the Multi-language tutorial and came up with the following routes.yaml file:

routes:

collections:
  /:
    permalink: /{slug}/
    template: index
    filter: 'tag:-hash-zh'
  /zh/:
    permalink: /zh/{slug}/
    template: index-zh
    filter: 'tag:hash-zh'

taxonomies:
  tag: /tag/{slug}/
  author: /author/{slug}/

This was uploaded via Labs and in desperation, I also restarted ghost several times.

However, the filters do not seem to have any effect at all. On both / and zh/ I see all the posts (i.e. those without tags plus those tagged with “#zh”).

Any suggestions on how I can debug this further?

  • Ghost version: 2.38.2

Hey @mtabz :wave:
The routes file you’ve made appears to be right. Have you uploaded a new index-zh.hbs for the /zh/ route to use?

Hi @DavidDarnes

Yes I’ve uploaded an index-zh.hbs file. It seems to be picked up correctly because I included the conditional HTML language template (as per the tutorial):

{{!< default}}
{{!-- The tag above means - insert everything in this file into the {body} of the default.hbs template --}}
{{#contentFor "lang"}}zh{{/contentFor}}

and the blog posts correctly contain the <html lang="zh"> tag.

Hey @mtabz, are you still having problems with this? Would you be willing to share your site so we can debug it? :blush:

Hey @DavidDarnes, yes this is still an issue. Site is here and expecting ZH articles to be filtered here

Is there a reason why you’re still running Ghost v2?

There have been a lot of changes and improvements to dynamic routing in 3.x and all our tutorials assume you are running the latest version.

It might not fix it, but it’s a good place to start.

1 Like

Simply a lack of bandwidth… We had done quite a lot of customisations on our current theme (based on Ghost v2) and so wanted to defer an upgrade to Ghost v3 as we weren’t sure whether there would be any incompatibilities between the theme and Ghost v3.

If there’s no ready/obvious fix to this issue then yes we’ll bump up the priority of the update to Ghost v3 to see if that fixes it.

Assuming that:

  • your routes.yaml is exactly as shown here
  • you have correctly uploaded routes.yaml (or changed it on the server AND restarted Ghost)
  • you have properly tagged all the posts

Then it should be working. Can confirm it works for me locallly on latest Ghost.

We had done quite a lot of customisations on our current theme (based on Ghost v2) and so wanted to defer an upgrade to Ghost v3 as we weren’t sure whether there would be any incompatibilities between the theme and Ghost v3.

I was thinking about what you said here since I first replied to you, and I wanted to write you another reply about how this is a common misconception about major version changes - and now I’ve seen Error starting Ghost after upgrade from 2.38 to 3.22: Cannot read property 'toString' of undefined - #4 by Hannah you’ve kinda proved the point I was going to make.

When it comes to major software versions, holding off upgrading because you’re worried about incompatibility is misconceived! The longer you hold off, the bigger the gap becomes between the software you are using and the latest version and the more changes (and potential incompatibilities) you’re dealing with.

Actually, the best time to upgrade is once the first minor goes out. E.g. once 3.1.0 is shipped.

Upgrading direct to a major like 3.0.0 means you’ll get caught out in any initial bugs, so holding off one minor gives you a little more confidence that any major unexpected compatibility issues have been solved.

Not only that, but this is the time when the core team and the community are most context loaded about compatibility and therefore most able to quickly help. 10 months on, noone remembers what v2 was like!

This doesn’t just apply to Ghost nor is it specific to you - just a passing thought I wanted to share.

1 Like

Thanks for the feedback on upgrade policy @Hannah, definitely worth bearing in mind for the future.

After my slight detour to upgrade-land :slight_smile:, the blog is now upgraded to v3.22.1.

Sadly, doesn’t seem to have helped with the filtering.

It’s almost as if it’s ignoring the filter conditions as whatever I put in there doesn’t seem to have any effect. E.g. if I change the filter for the default collection to show some arbitrary (and non-existent) tag like filter: 'tag:faketag' then I’d expect to see no blog posts in that collection. But even with such a filter I still get all the posts appearing on the homepage.

Is there a way to debug the routes/filters to see what values are actually being used?

Can you share the exact steps you’re using to change your routes.yaml file, and the theme code for you index and index-zh templates?

Sure. I change routes.yaml as follows:

  • Edit routes.yaml file locally in a text editor
  • Upload file via Admin > Labs > Routes > “Upload routes YAML”
  • For good measure issue ghost restart

I also tried (no effect either):

  • Edit routes.yaml as before
  • Place routes.yaml into ghost/content/settings/
  • ghost restart

Contents of index.hbs verbatim:

{{!< default}}
{{!-- The tag above means - insert everything in this file into the {body} of the default.hbs template --}}
{{#contentFor "lang"}}{{@site.lang}}{{/contentFor}}

{{!-- The tag below includes the theme header - partials/header.hbs --}}
{{> header}}

{{!-- The content area on the blog page --}}
<div id="content" class="site-content">
        <main id="main" class="site-main outer">

                {{!-- The tag below includes the post loop - partials/loop.hbs --}}
                {{> "loop"}}

        </main><!-- .site-main -->
</div><!-- .site-content -->

And index-zh.hbs:

{{!< default}}
{{!-- The tag above means - insert everything in this file into the {body} of the default.hbs template --}}
{{#contentFor "lang"}}zh{{/contentFor}}

{{!-- The tag below includes the theme header - partials/header.hbs --}}
{{> header}}

{{!-- The content area on the blog page --}}
<div id="content" class="site-content">
        <main id="main" class="site-main outer">

                {{!-- The tag below includes the post loop - partials/loop.hbs --}}
                {{> "loop"}}

        </main><!-- .site-main -->
</div><!-- .site-content -->

To isolate this, I tried temporarily changing the theme to Casper and interestingly enough filtering started working.

So potentially there’s something in the theme, or how Ghost interacts with it, that is causing filtering to fail. Although I don’t know enough to debug it further.

Any thoughts on avenues to explore @Hannah?

What is in this file?

Here you go:

{{!-- The post loop with featured posts excluded --}}
{{#get "posts" include="tags" limit=@config.posts_per_page filter="featured:false" page=pagination.page}}

    <div class="post-feed inner-wide">
        {{#foreach posts visibility="all"}}
        <article class="{{post_class}}">
            <header class="post-header">
                <div class="post-meta">
                    <time class="published" datetime="{{date format='YYYY-MM-DD'}}">{{date format="MMMM DD, YYYY"}}</time>
                </div><!-- .post-meta -->
                <h2 class="post-title"><a href="{{{url}}}" rel="bookmark">{{title}}</a></h2>
            </header><!-- .post-header -->
            {{#if feature_image}}
            <a href="{{url}}" class="post-thumbnail"><img srcset="{{img_url feature_image size="xxs"}} 150w, {{img_url feature_image size="xs"}} $
            {{/if}}
            <div class="post-content">
                {{#if custom_excerpt}}
                <p>{{custom_excerpt}}</p>
                {{else}}
                <p>{{excerpt words="40"}}&hellip;</p>
                {{/if}}
                {{#if tags}}
                <p class="post-tags">{{tags separator=" "}}</p>
                {{/if}}
            </div><!-- .post-content -->
        </article><!-- .post -->
        {{/foreach}}
    </div><!-- .post-feed -->

    {{!-- Previous/next page links - displayed on every page --}}
    {{pagination}}

{{/get}}

Right so there you go… your templates aren’t using dynamic routing. They’re doing their own local get request to fetch tags with the filter featured:false.

Ah, that makes sense. Thanks for that!

(As you might have guessed we are using a 3rd party theme which we customised. I’ll flag this to the theme provider.)