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

**URL:** <https://forum.ghost.org/t/get-random-post-using-api-without-pulling-lots-of-them-first-solved/46250>\
**Category:** Integrations & API\
**Created:** [April 2, 2024, 12:25pm UTC](https://forum.ghost.org/t/get-random-post-using-api-without-pulling-lots-of-them-first-solved/46250 "2024-04-02T12:25:50Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![hafidzaini](https://sea1.discourse-cdn.com/flex015/user_avatar/forum.ghost.org/hafidzaini/32/20043_2.png) [@hafidzaini](https://forum.ghost.org/u/hafidzaini)\
**Post date:** [April 2, 2024, 12:25pm UTC](https://forum.ghost.org/t/get-random-post-using-api-without-pulling-lots-of-them-first-solved/46250/1 "2024-04-02T12:25:50Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Cathy\_Sarisky](https://sea1.discourse-cdn.com/flex015/user_avatar/forum.ghost.org/cathy_sarisky/32/25750_2.png) [@Cathy\_Sarisky](https://forum.ghost.org/u/Cathy_Sarisky)\
**Post date:** [April 2, 2024, 12:31pm UTC](https://forum.ghost.org/t/get-random-post-using-api-without-pulling-lots-of-them-first-solved/46250/2 "2024-04-02T12:31:09Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![hafidzaini](https://sea1.discourse-cdn.com/flex015/user_avatar/forum.ghost.org/hafidzaini/32/20043_2.png) [@hafidzaini](https://forum.ghost.org/u/hafidzaini)\
**Post date:** [April 2, 2024, 12:51pm UTC](https://forum.ghost.org/t/get-random-post-using-api-without-pulling-lots-of-them-first-solved/46250/3 "2024-04-02T12:51:06Z")

</div>

yes, but i should pull lots of post first 🙂  
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? 🤔

---

<div class="post-metadata">

**Author:** ![Cathy\_Sarisky](https://sea1.discourse-cdn.com/flex015/user_avatar/forum.ghost.org/cathy_sarisky/32/25750_2.png) [@Cathy\_Sarisky](https://forum.ghost.org/u/Cathy_Sarisky)\
**Post date:** [April 2, 2024, 1:31pm UTC](https://forum.ghost.org/t/get-random-post-using-api-without-pulling-lots-of-them-first-solved/46250/4 "2024-04-02T13:31:07Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![hafidzaini](https://sea1.discourse-cdn.com/flex015/user_avatar/forum.ghost.org/hafidzaini/32/20043_2.png) [@hafidzaini](https://forum.ghost.org/u/hafidzaini)\
**Post date:** [April 2, 2024, 1:44pm UTC](https://forum.ghost.org/t/get-random-post-using-api-without-pulling-lots-of-them-first-solved/46250/5 "2024-04-02T13:44:35Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![hafidzaini](https://sea1.discourse-cdn.com/flex015/user_avatar/forum.ghost.org/hafidzaini/32/20043_2.png) [@hafidzaini](https://forum.ghost.org/u/hafidzaini)\
**Post date:** [April 2, 2024, 1:49pm UTC](https://forum.ghost.org/t/get-random-post-using-api-without-pulling-lots-of-them-first-solved/46250/6 "2024-04-02T13:49:50Z")

</div>

`&limit=1&from=7`

from always show the last post

---

<div class="post-metadata">

**Author:** ![vikaspotluri123](https://sea1.discourse-cdn.com/flex015/user_avatar/forum.ghost.org/vikaspotluri123/32/24_2.png) [@vikaspotluri123](https://forum.ghost.org/u/vikaspotluri123)\
**Post date:** [April 2, 2024, 3:13pm UTC](https://forum.ghost.org/t/get-random-post-using-api-without-pulling-lots-of-them-first-solved/46250/7 "2024-04-02T15:13:00Z")

</div>

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`

---

<div class="post-metadata">

**Author:** ![hafidzaini](https://sea1.discourse-cdn.com/flex015/user_avatar/forum.ghost.org/hafidzaini/32/20043_2.png) [@hafidzaini](https://forum.ghost.org/u/hafidzaini)\
**Post date:** [April 2, 2024, 3:35pm UTC](https://forum.ghost.org/t/get-random-post-using-api-without-pulling-lots-of-them-first-solved/46250/8 "2024-04-02T15:35:33Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![vikaspotluri123](https://sea1.discourse-cdn.com/flex015/user_avatar/forum.ghost.org/vikaspotluri123/32/24_2.png) [@vikaspotluri123](https://forum.ghost.org/u/vikaspotluri123)\
**Post date:** [April 2, 2024, 3:48pm UTC](https://forum.ghost.org/t/get-random-post-using-api-without-pulling-lots-of-them-first-solved/46250/9 "2024-04-02T15:48:46Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![hafidzaini](https://sea1.discourse-cdn.com/flex015/user_avatar/forum.ghost.org/hafidzaini/32/20043_2.png) [@hafidzaini](https://forum.ghost.org/u/hafidzaini)\
**Post date:** [April 2, 2024, 4:03pm UTC](https://forum.ghost.org/t/get-random-post-using-api-without-pulling-lots-of-them-first-solved/46250/10 "2024-04-02T16:03:25Z")

</div>

i mean limiting API request post count for pagination.

> <https://github.com/TryGhost/Ghost/issues/5093>
>
> This is needed in order to fully support channel customisations, see #5091 
> 
> \---…
> 
> As part of the move towards refactoring the Ghost frontend into a fully customisable and extensible set of channels, one of the features or customisations we want to make available is the ability to set a different number of posts-per-page for the first page vs subsequent pages in a channel.
> 
> E.g. make it so that the homepage shows 1 post, and every subsequent page in the index channel shows 10. 
> 
> At the moment, pagination in the API is managed by passing in a \`limit\` (how many posts per page) and a \`page\` number. This enforces that every page in the set must be the same size. This limitation can be overcome by telling the API a \`limit\` (how many posts you want) and an \`offset\` (where in the list to start from). Calculations around the required offset for a given page can then be done based on the channel settings.
> 
> Therefore, we should replace the \`page\` concept in the API with an \`offset\` concept instead, for all of our findPage methods. 
> 
> See also #2896

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

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 🤔

---

<div class="post-metadata">

**Author:** ![vikaspotluri123](https://sea1.discourse-cdn.com/flex015/user_avatar/forum.ghost.org/vikaspotluri123/32/24_2.png) [@vikaspotluri123](https://forum.ghost.org/u/vikaspotluri123)\
**Post date:** [April 2, 2024, 4:15pm UTC](https://forum.ghost.org/t/get-random-post-using-api-without-pulling-lots-of-them-first-solved/46250/11 "2024-04-02T16:15:42Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![hafidzaini](https://sea1.discourse-cdn.com/flex015/user_avatar/forum.ghost.org/hafidzaini/32/20043_2.png) [@hafidzaini](https://forum.ghost.org/u/hafidzaini)\
**Post date:** [April 2, 2024, 4:34pm UTC](https://forum.ghost.org/t/get-random-post-using-api-without-pulling-lots-of-them-first-solved/46250/12 "2024-04-02T16:34:09Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Kevin](https://sea1.discourse-cdn.com/flex015/user_avatar/forum.ghost.org/kevin/32/32_2.png) [@Kevin](https://forum.ghost.org/u/Kevin)\
**Post date:** [April 2, 2024, 4:44pm UTC](https://forum.ghost.org/t/get-random-post-using-api-without-pulling-lots-of-them-first-solved/46250/13 "2024-04-02T16:44:19Z")

</div>

@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:

```auto
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});

```

---

<div class="post-metadata">

**Author:** ![vikaspotluri123](https://sea1.discourse-cdn.com/flex015/user_avatar/forum.ghost.org/vikaspotluri123/32/24_2.png) [@vikaspotluri123](https://forum.ghost.org/u/vikaspotluri123)\
**Post date:** [April 2, 2024, 5:12pm UTC](https://forum.ghost.org/t/get-random-post-using-api-without-pulling-lots-of-them-first-solved/46250/14 "2024-04-02T17:12:04Z")

</div>

Ahh, I completely forgot that the pagination object includes the total resource count 🤦‍♂️

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

(pseudocode)

```ts
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];
}

```

---

<div class="post-metadata">

**Author:** ![hafidzaini](https://sea1.discourse-cdn.com/flex015/user_avatar/forum.ghost.org/hafidzaini/32/20043_2.png) [@hafidzaini](https://forum.ghost.org/u/hafidzaini)\
**Post date:** [April 2, 2024, 5:25pm UTC](https://forum.ghost.org/t/get-random-post-using-api-without-pulling-lots-of-them-first-solved/46250/15 "2024-04-02T17:25:12Z")

</div>

interesting…  
thank you very much for your the code 🙏

---

<div class="post-metadata">

**Author:** ![hafidzaini](https://sea1.discourse-cdn.com/flex015/user_avatar/forum.ghost.org/hafidzaini/32/20043_2.png) [@hafidzaini](https://forum.ghost.org/u/hafidzaini)\
**Post date:** [April 2, 2024, 5:26pm UTC](https://forum.ghost.org/t/get-random-post-using-api-without-pulling-lots-of-them-first-solved/46250/16 "2024-04-02T17:26:15Z")

</div>

thank you very much @Kevin .  
will try that code 👍

---

<div class="post-metadata">

**Author:** ![hafidzaini](https://sea1.discourse-cdn.com/flex015/user_avatar/forum.ghost.org/hafidzaini/32/20043_2.png) [@hafidzaini](https://forum.ghost.org/u/hafidzaini)\
**Post date:** [April 2, 2024, 6:11pm UTC](https://forum.ghost.org/t/get-random-post-using-api-without-pulling-lots-of-them-first-solved/46250/17 "2024-04-02T18:11:05Z")

</div>

codes above are working great!  
tiny and fast!  
thank you @Kevin and @vikaspotluri123 🙏 👍

---

<div class="post-metadata">

**Author:** ![gillc](https://sea1.discourse-cdn.com/flex015/user_avatar/forum.ghost.org/gillc/32/30786_2.png) [@gillc](https://forum.ghost.org/u/gillc)\
**Post date:** [June 9, 2024, 5:10pm UTC](https://forum.ghost.org/t/get-random-post-using-api-without-pulling-lots-of-them-first-solved/46250/18 "2024-06-09T17:10:11Z")

</div>

Thank you so much for the tips @Kevin and @vikaspotluri123. I was able to get this working on my site here -

> **[Discover](https://punjabisahit.com/discover/)**
>
> A Literary Voyage Through Time

Now I’m struggling with formatting. I want all these random posts to be shown in boxes like on the homepage. What’s the best way to format the html and css to match the theme styling?
