Get random post using API without pulling lots of them first (Solved)

Hello,
Btw, is there any way to get 1 or 2 random post without pulling lots of the post first?
thanks

in Handlebars, no. But you could use Javascript to talk to the Content API to do this.

1 Like

yes, but i should pull lots of post first :slight_smile:
i need to do it with just tiny resource.

even pulling based on tag, it will use lots of resource.

or can it be pulled based on day the post publish? :thinking:

You can pull one single post from the content API with javascript. Generate a random number and then pass in from={your random value} and limit=1. That won’t pull additional posts.

If you want random-ish, I think that’s your best bet. Sure, you could use publication date or something to get the closest post - if that actually meets your purposes, check prev/next helpers, which will probably be easier than writing your own!

1 Like

yes, that i was thinking.
but how to get post count? i cant find any info on admin panel.

so the solution is to pull all post to know the the post count?

&limit=1&from=7

from always show the last post

I’m not sure if there’s an endpoint just for that, but if you want to limit the data usage, you can use limit the fields that are included:

/ghost/api/content/posts/?limit=all&fields=featured

You can easily cache this data for some time to avoid expensive lookups.

For the offset, try &limit=1&page=7

1 Like

oh… thats e good idea.

‘&limit=1&page=7’

to get the page.

how about limiting post per page?
is there any feature for this?

so the resource will be tiny.
i want to grab the data on Cloudflare Worker.
so i need it as tiny as possible. so it will be lightning fast

LMAO

Which posts per page are you trying to limit? For the global (post count) request, or the local (random post) one?

For the global, that’s why the filter specifies fields=featured, to avoid including a large amount of data. If you limit the number of posts, the post count in the meta object will also be limited.

For the local, you’re already limiting the posts per page to 1

1 Like

i mean limiting API request post count for pagination.

ok then.
thank you very much for all who reply. giving me something to think about :pray:.

maybe i’ll just save the post ID to Cloudflare KV somewhere else to get it randomised, and fetch one.

might be better if Ghost CMS API could just fetch some post field only and not all the fields :thinking:

I’m still not understanding your question. The data we’ve shared should definitely be enough to create a performant randomizer, with the only stateful part being the cached post count

1 Like

yes.i mean.
feature filter is not enough.

let say if i have thousand or ten thousand post.
may be i will just save the post ID somewhere else to get random post with little resource.

or i fetch all the post json weekly, then feed Cloudflare worker with 1 random post. :thinking:

@hafidzaini you don’t need to fetch all of the posts, you fetch 1 post and use the total included in the metadata to work out your random range.

Pseudocode:

const countFetch = fetchPosts({limit: 1});
const total = countFetch.meta.pagination.total; // you can cache this count
const randomPageNo = Math.floor(Math.random() * total) + 1;
const randomFetch = fetchPosts({limit: 1, page: randomPageNo});
3 Likes

Ahh, I completely forgot that the pagination object includes the total resource count :man_facepalming:

On top of that, you can use the randomFetch to keep the cache (relatively) up to date:

(pseudocode)

let cachedPostCount;

function setup() {
  cachedPostCount = fetchPosts({limit: 1}).meta.pagination.total;
}

function randomize() {
  const postNumber = Math.floor(Math.random() * cachedPostCount) + 1;
  const randomFetch = fetchPosts({limit: 1, page: randomPageNo});
  cachedPostCount = randomFetch.meta.pagination.total;
  return randomFetch.posts[0];
}
2 Likes

interesting…
thank you very much for your the code :pray:

thank you very much @Kevin .
will try that code :+1:

codes above are working great!
tiny and fast!
thank you @Kevin and @vikaspotluri123 :pray: :+1:

1 Like