Querying Posts in a given time range via content API

Hi,

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?

Kind regards,
Christoph

You can use the ?filter param for that. Eg:

GET /ghost/api/v2/content/posts/?filter="published_at:>='1948-12-12 00:00:00'+published_at:<='1963-08-28 23:50:59'"

Note that dates need to wrapped in quotes inside the filter param, also make sure that everything gets url-encoded properly :slight_smile:

➜ curl https://demo.ghost.io/ghost/api/v2/content/posts/\?key\=22444f78447824223cefc48062\&\filter\="published_at:>='1948-12-12%2000:00:00'%2bpublished_at:<='1963-08-28%2023:59:59'"\&fields=title,published_at | jq
######################################################################## 100.0%
{
  "posts": [
    {
      "title": "I Have a Dream",
      "published_at": "1963-08-28T08:00:00.000+00:00"
    },
    {
      "title": "The Purpose of Education",
      "published_at": "1948-12-12T10:00:00.000+00:00"
    }
  ],
  "meta": {
    "pagination": {
      "page": 1,
      "limit": 15,
      "pages": 1,
      "total": 2,
      "next": null,
      "prev": null
    }
  }
}

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'`

image

(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'`

Thank you and hope to hear from you soon! :)

I’m not able to test this at the moment but these are the steps I’d take to troubleshoot this.

  1. Does the API call work without the filter param? This will help us pinpoint that it’s the point of failure.

  2. What happens if you add time to your date string?

  3. What happens if you use published_at instead of created_at?

Not sure if these things will solve your, but I think they’ll help you get closer.

Hi. thank you @TheRoyalFig for your fast response!

  1. Yes, it does work without the filter param.
			`${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}`

  1. 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
  1. 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.

Thanks and hope to hear from you soon.

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`
		

That works, but you would want to encode the entire filter :slightly_smiling_face:

Alternatively, the Content API SDK does all this work for you:

2 Likes