Getting the Next & Previous Posts through the Ghost API

I have a SPA (EmberJS) where I’m calling the API and would like to get the Next and Previous post. Is there a simple way to do this?

return fetch(`https://*****.ghost.io/ghost/api/v2/content/posts/?key=*****&include=tags&limit=10`).then(response => {
    return response.json();
});

@Mithrilhall what do next and previous refer to in your context? I’m assuming you have a post otherwise next/previous doesn’t make a great deal of sense but do you want to find next/prev with particular tags, within all posts, inside a specific group of posts?

FYI, a good reference for how you may do it is the {{prev/next}} helper source https://github.com/TryGhost/Ghost/blob/master/core/frontend/helpers/prev_next.js

@Kevin, I do indeed have a post. I’m looking for the next/prev within all posts (by publish date I suppose).

@Kevin, is this possible from within something like Postman?

I probably should have put in the original post that I’m trying to do this with the content api.

Something like this would work…I’m just not sure on the syntax around the date:

https://<custom>.ghost.io/ghost/api/v2/content/posts/?key=<key>&include=author,authors,tags&filter=slug:-donec-dui-tellus+published_at:<=72019-06-10

Edit:

I think I figured out what I needed:

https://<custom>.ghost.io/ghost/api/v2/content/posts/?key=<key>&include=author,authors,tags&filter=published_at:<=2019-06-28T13%3A23%3A07%2Bpublished_at:>=2019-06-18%2Bslug:-tortor-mattis&limit=2

@Mithrilhall

Did you finally get the next & prev post ?

I try with

https://<custom>.ghost.io/ghost/api/v2/content/posts/?key=<key>&include=author,authors,tags&filter=published_at:<=2019-06-28T13%3A23%3A07%2Bpublished_at:>=2019-06-18%2Bslug:-tortor-mattis&limit=2

But API seems to be not recognizing the Date format

I am using aditional parameters

?filter=tags:['tag','tag2','tag3']

@damr67, is there a reason you’re not using the Ghost SDK to generate urls? Your url is not correctly formatted which is why you’re running into issues. For example, the < symbol is illegal

@damr67

I performed two fetches using the following URLs.

Next was this:
https://aspensquare.ghost.io/ghost/api/v2/content/posts/?key=<key>&include=author,authors,tags&filter=published_at:>${published_at}%2Bslug:-${params.slug}&order=published_at&limit=1

Previous was this:
https://aspensquare.ghost.io/ghost/api/v2/content/posts/?key=<key>&include=author,authors,tags&filter=published_at:<=${published_at}%2Bslug:-${params.slug}&limit=1

I hope this helps.

1 Like

Thanks @Mithrilhall

I hope this helps

:D :ok_hand:Helps a lot.

I did 2 api requests as well + The blog Post itseltf.

Please do not forget to add

&order=published_at%20desc

at the end of your query if you want results from newest to oldest articles. Some times api returns unexpected results

1 Like