Unclear documentation on creating filters

Hello :wave: trying out Ghost I’m a bit confused by the “collections” documentation. I’m not sure if I’m just not understanding it, or if there is a mistake in the documentation.

On this page under Filtering Posts it gives the following example filter.

collections:
  /french/:
    permalink: /french/{slug}/
    template: cuisine
    filter: tag:french
  /:
    permalink: /{slug}/
    template: index
    filter: tag:-french

This puts all the posts with the tag “french” on the /french/ route. Makes sense.

But under there it has the following note:

Order matters! If these collections were reversed with /french/ coming after /: , you wouldn’t achieve the desired result because all the posts would be collected on the default route (homepage) and there wouldn’t be anything left to show on /french/ .

If that is the case, what is the point of the filter on the / route to say filter: tag:-french.

As I understand it, the filter filter: tag:-french means “anything not tagged french”. But according to the note above, that should have already been taken care of by the preceding filter (i.e. the only thing left by that point should be things not tagged french, because all the things tagged french are in another route). So is that second filter redundant?

Just trying to get my head around how this works, thanks!

Bad documentation! :)

Thanks for flagging this :slightly_smiling_face:

You’re right on the ball with your understanding.

In revision, two approaches got combined, which made it unclear (basically, adding that inverse filter). It’s now been revised for clarity.

The approach to take is to define your collections and then use an inverse filter to exclude those posts from the default collection. More about why is here: Filter property not working as expected in route.yaml - Ghost Developers

Thanks @Cathy_Sarisky @RyanF that’s good to hear. The inverse filters makes this more flexible, which is great.

The Filter property not working in routing page seems to have the same issue in the order filters section. It says

In a collection, the post lives in the first collection where it matches the filter, so it’s important to consider the order of your filters and place more generic filters towards the end. If you are seeing unexpected behaviour and posts living inside the wrong collection, then you may need to revise the order of your filters.

But if I understand it right, order doesn’t matter (any more, guess this was an earlier iteration) and posts appear in collections according only to the collection’s own filters.

1 Like

If you’ve covered all your bases with inverse filters, then you’re right that order shouldn’t matter. It’s also the better approach to avoid any kind of edge cases.

However, order will matter if you don’t use inverse filters. There may be some use case that opts for this approach or sees odd behavior, which is the reason for that note on order.

Oh, now I’m confused again :slight_smile:

Are you saying that previous collections do filter things out from later collections? Or only if you don’t have negative filters set on your main collection? (i.e. does it detect if you’re using negative filters).

As an example –

collections:
  /french/:
    permalink: /french/{slug}/
    template: cuisine
    filter: tag:french
  /italian/:
    permalink: /french/{slug}/
    template: cuisine
    filter: tag:italian
  /:
    permalink: /{slug}/
    template: index
    filter: tag:-french

Would Italian recipes show up on the homepage with this structure? If not, what is the purpose of the negative filters on /, it’s redundant no? If yes, what is the effect of order?

collections:
  /french/:
    permalink: /french/{slug}/
    template: cuisine
    filter: tag:french
  /spicy/:
    permalink: /french/{slug}/
    template: cuisine
    filter: tag:spicy
  /:
    permalink: /{slug}/
    template: index
    filter: tag:-french

If there was spicy French food (!) would the recipe appear in French, Spicy or both? Would spicy food appear on the homepage?

Collections are exclusive. A post can’t live in more than one collection, which means that order does matter.

In your first example, italian posts wouldn’t be available in the default collection because they already live on the italian route. However, to ensure that pagination works correctly, it’s best to also specify the inverse filters: tag:-[french,italian]. Pagination means that posts show up correctly on french/, french/page/2, etc.

An example of where pagination might get weird is this:
If you assign collections to French and Spicy but don’t assign an inverse filter (tag:-[french,spicy]), then you might find empty pages on your homepage.

This is because, while French and Spicy posts no longer live on the default collection, they haven’t been explicitly excluded from it. Ghost doesn’t know not to count them and so you have pagination with empty pages.

1 Like

A post can be in only one collection. If you want to have spicy French food, you might want to look at channels instead! :slight_smile:

Collections /should/ take posts out in the order they appear, but the behavior can be ‘quirky’, especially with pagination. Adding the negative filter can help avoid that behavior.

2 Likes

Ah, that’s the ticket. I think channels is what I was looking for!

Thanks both of you for the help, much appreciated.

2 Likes