gatsby-starter-ghost icon indicating copy to clipboard operation
gatsby-starter-ghost copied to clipboard

Related Posts plugin

Open aileen opened this issue 6 years ago • 4 comments

Problem description

It is possible to fetch related posts based on how many tags they have in common. A current implementation of that can be found in the Ghost Docs repository here. This code is very specific to the Ghost Docs requirements and also very messy.

There are probably better solutions to solve this, but this one works and can be a good starting point to create a plugin for the

Proposal

I had a play with this already, trying to add related posts to the GraphQL schema, but wasn't successful. This might be due to my lack of GraphQL knowledge, or simply because it's not possible that way.

The working solution in Ghost Docs inserts the related posts into the PageContext which is another option (not such a nice one, as having it available with a pretty GraphQL query) to implement this.

Maybe (probably) there are even better ways on doing that? This needs further research.

Todos

  • [ ] investigate do-ability and different options further
  • [ ] write up tech spec and agree on solution with core team
  • [ ] implement a general plugin for related Ghost posts, that can be configured
  • [ ] publish plugin

aileen avatar Jan 14 '19 07:01 aileen

Any update on this ?, How we can show related posts on frontend just like ghost's casper theme ?

imballa avatar Nov 20 '19 13:11 imballa

See my casper-v3 fork for a solution. The trick is to use grapghql queries in post.js like so:

        post: ghostPost(slug: { eq: $slug }) {
            ...GhostPostFields
        }
        prev: ghostPost(slug: { eq: $prev }) {
            ...GhostPostFields
        }
        next: ghostPost(slug: { eq: $next }) {
            ...GhostPostFields
        }

which can be accessed with data.post, data.prev and data.next. The query parameters are computed in gatsby-node.js. This solution is very clean and does not pollute the PageContext.

styxlab avatar Feb 19 '20 19:02 styxlab

@styxlab your link to your Casper fork is broken. I am really blocked as I would like to list all posts having the same tags as current post. Where you be able to solve that? Thanks

fabianstarke avatar Mar 24 '20 23:03 fabianstarke

for anyone looking for a hacky solution to this, i've been able to get related posts showing image

I'm not a GraphQL expert and this isn't a plugin but you can find the code used here. I'm basically using the allGhostPost query and then filtering that on the related tags manually, ideally you'd use the filter property inside that query but I couldn't figure out how to pass in the parameter to filter on.

It's not the greatest but I hope it helps someone who was stuck like me.

javaadpatel avatar Aug 30 '20 08:08 javaadpatel