I’m currently looking for a possibility to get all Posts of a given time range via the content api. The scenarios I have in mind are the following:
Get all Posts from the last x days
Get all Posts between and
Get all Posts from month
Get all Posts older than x
Get all Posts younger than x
After reading the documentation does not seem to be a parameter to adjust GET Post queries in this way. Is there any other possibility than fetching all posts and doing the math ony my own?
Hi @Kevin, thank you for response regarding this despite being over two years ago. I have followed and researched on how to get this querying as well but could not get the outcome needed (such as getting a range of dates). To note, I am using Ghost v4 and TypeScript.
const res = await fetch(
`${BLOG_URL}/ghost/api/v4/content/posts/?key=${CONTENT_API_KEY}
&fields=title,slug,custom_excerpt,feature_image,created_at,tag
&limit=${setLimit}&page=${page}
&filter=created_at:>='${setFilter}-01-01'+created_at:<='${setFilter}-12-31'`
(the ${setFilter} variable has already been stringifyed.)
I have tried adding " " to the filter, created_at:[>='${setFilter}-01-01', created_at:<='${setFilter}-12-31], and remove the single quotations &filter=created_at:>=${setFilter}-01-01+created_at:<=${setFilter}-12-31 but none of them would work. It only works when I just fetch &filter=created_at:>='${setFilter}-01-01' but not the +created_at:<='${setFilter}-12-31'`
With the time added, it still works but when I add another filter, it does not work. It returns the TypeError: Failed to fetch error like the last time and displays no posts on the front-end.
&filter=created_at:>='${setFilter}-01-01 00:00:00' //this works
&filter=created_at:>='${setFilter}-01-01 00:00:00'+created_at:<='${setFilter}-12-31 00:00:00' //this does not work
I also had tried published_at before and I still encountered the same problem with the second question.
&filter=published_at:>='${setFilter}-01-01 00:00:00' // this works
&filter=published_at:>='${setFilter}-01-01 00:00:00'+published_at:<='${setFilter}-12-31 00:00:00' //this does not
So I am assuming that my problem might be in the operands of adding the filter? But I have already tried several ways such as &filter=created_at:[>='${setFilter}-01-0', <='${setFilter}-12-31' and making sure that the API is connect (which already is). Not sure what else I can do since I have searched throughout the documentation and forums but not enough information to understand the filtering process.
I don’t see any url encoding logic in your request. Are you adding it elsewhere? The + sign is a special character in urls (meaning it could be misinterpreted), so that could be why you’re running into issues
Hi @vikaspotluri123, thank you for your reply! Yes, you’re right, its about the encoding characters to url that makes things wacky! I apologize for my limited practice and understanding on how encoding should work, the solution I give down below is a bit hacky (can also just add with %2B insead of using encodeURIComponent). But thank you for directing me on what’s wrong!
const plus = encodeURIComponent(`+`)
const res = await fetch(`${BLOG_URL}/ghost/api/v4/content/posts/?key=${CONTENT_API_KEY}&fields=title,slug,custom_excerpt,feature_image,created_at,tag&limit=${setLimit}&page=${page}&filter=created_at:>=${setFilter}-01-0`+plus+`created_at:<=${setFilter}-12-31`