Help with primary tag/tags in Ghost + Eleventy 11ty + Content API

I have built a site using self-hosted headless Ghost as a backend and Eleventy as a static site frontend. I used the (official?) Eleventy Starter Ghost repo that is referenced in Ghosts’s own docs on building a site with Ghost + Eleventy.

I need help with how the eleventy.js file, which accesses the Ghost Content API constructs the tag collection for Eleventy:

within:
config.addCollection("tags", async function(collection)
this snippet of code populates each tag with all the posts that have that tag as their primary_tag

  // Attach posts to their respective tags
  collection.map(async tag => {
    const taggedPosts = posts.filter(post => {
      return post.primary_tag && post.primary_tag.slug === tag.slug;
    });

The full code is included and explained in the Jamstack Eleventy + Ghost doc page.

I would like some help to instead populate my Eleventy tag collection such that each tag item has every post with that tag attached to it (not just those with it as their primary_tag. The javascript here is a bit too dense for me to follow and modify, and I’m not familiar enough with the Ghost Content API JS client in use. I’m hoping for some help and direction on how to change this.

I did quite a bit of searching on this, and there is a github issue with the same question on the eleventy-ghost-starter repo that was closed by the maintainer who directed such inquiries to this forum. I hope this is the right place. This issue could belong in many different courts, the question is about Eleventy code technically, but I think because it’s about using Ghost in the officially-provided-by-Ghost documentation and starter it belongs here?

I think it would also be useful to others building a site with this stack, since I think it makes intuitive sense to have that collection generated from Ghost content when building a site like this. I’m not sure why primary_tag is used here.

I had included more links in this topic, but because this is my first post I have to remove several.

An additional link I had to omit from the original post. Here is the official Ghost doc page on this stack/ using this starter repo that includes the same code referenced here: Build A Custom Publication With Headless Ghost + Eleventy

OK, so it looks like you’re filtering posts based on tag. When the return of that filter is ‘true’, you end up with the post getting attached. So you need to modify the filter to be true for any time the tag is attached.

so something like

return post.primary_tag && post.tags.some( onetag => onetag.slug === tag.slug)

I left the first part of the return the same, because a post with no tags will have no primary tag.

1 Like

Thank you @Cathy_Sarisky ! That appears to have worked flawlessly.

1 Like