Filter out posts by tag on homepage

Hello! I’m a newbie to ghost and not very advanced with coding either… That said, in the Liebling theme, I would like posts of a certain tag to NOT be displayed in the loop on the homepage, but just on the “tag page”. Is there a simple way to do this? Thank you!

Hi @jonahl,

welcome to ghost community :smiley:

You can do that via the routes.yaml in your Ghost Admin > Settings > Labs:

  1. Download your routes.yaml
  2. Add a filter under collections, like shown in the screenshot:
    image
    tag = means you want to filter by tag
    -yourcustomtag = means, that posts with yourcustomtag get excluded.
    You can combine multiple tag filters, by adding a plus sign
    Make sure to add the hyphen before your tag, otherwise your page will only show posts with that certain tag

More info in the ghost docs:

Let me know if you need help :stuck_out_tongue:

2 Likes

Hey, @daniel1. Thank you so much, that works! I really appreciate it—I feel like a new world has been opened up… :wink: :raised_hands:

Hi @daniel1 , I’ve done this. I want my blog frontpage to omit posts with the “wiki” tag, but my wiki page to include only posts with the “wiki” tag. So my solution is this:
image

However now I have everything on the blog page, and nothing on the Wiki page. Although I have several posts tagged as only wiki.

What am I doing wrong?

1 Like

This is so cool. I added the

filter: tag:-mytag

And it worked in the home page.
However, navigating to that tag’s page, I click on a post with that tag and I get a 404. Any ideas here? @daniel1

Thanks for the help!

@pewu, the reason you get a 404 is that posts have to be in some collection in order to have a URL at all, because it’s the collection’s permalink property that determines a post’s URL.

To fix it, you need to not just remove posts with your tag from the index collection, but also create a new collection that you put them in. That new collection can still have the same permalink property as the index collection if you like, but its key in the collections dictionary (which is the URL where you can view the collection) has to be different. If you don’t want a page where you can view the collection (because you want to use the tag page at /tag/mytag instead), a hack that seems to work, at least for the current version of Ghost, is to just put some URL-illegal characters like ^ into the URL of the new collection. This, I think, will give you what you were looking for:

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

  /^/:
    permalink: /{slug}/
    template: index
    filter: tag:mytag