Filter posts in routes.yaml but get all data

Hello,
On my homepage, I would like to use the lopp only for posts that have a specific tag.
On the same page, I have sections that display other posts (posts which do not have this specific tag).

Index.hbs :

{{!< default}}
{{>featured}}
<div class="loop-section">
	{{>spotlight}}
	<div class="loop-wrap">
		<div>
			{{>loop}}
		</div>
	</div>
</div>
{{pagination}}
{{>sections}}

loop.hbs

{{#foreach posts}}
{{#has tag="articles"}}
{{>loop_item}}
{{/has}}
{{/foreach}}

spotlight.hbs

{{#get "posts" limit="1" filter="tag:hash-spotlight" include="authors,tags" as |spotlights|}}
{{#if spotlights}}
{{#foreach spotlights}}
{{>spotlight_item}}
{{/foreach}}
{{/if}}
{{/get}}

Problem on the homepage → the “more posts” button (for the loop) is displayed all the time, even when there are no other posts to load (not surprising as I filter after loading the data).

So i Tried to filter before in routes.yaml:
routes.yaml

routes:

collections:
  /:
    permalink: /{slug}/
    template: index
    filter: tag:articles

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

But now the homepage links for posts that don’t have the “articles” tag no longer work (there is pictures and titles, only slugs seems broken and replaced by a kind of id).

I wonder if there is a way to filter posts in routes.yaml but load the data as if I needed all the posts ?

With your current routes setup, you only include posts with the tag ‘articles’ in your main collection and collections change the URL of a post, if a post isn’t included in a collection then it has no URL.

You might try to leave the collection as it is and use a channel for your home page instead: Ghost Themes - Dynamic URLs & Routing

1 Like

Hello, thanks ! I didn’t know the channels, it will help me for other projects. For my problem, I was able to solve it by separating posts and pages … I think it’s easier. (loop = posts).

Hello, finally I only used posts… not pages (because I need to use collections). To filter the posts, I used like you said a channel for my homepage. It works, thank you very much! So i can filter my loop, and my pagination is ok !

routes:
  /:
    controller: channel
    filter: tag:articles

collections:
  /:
    permalink: /{slug}/
    template: index

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