# How order get helper items by field or randomly

**URL:** <https://forum.ghost.org/t/how-order-get-helper-items-by-field-or-randomly/9526>\
**Category:** Developer help\
**Created:** [October 15, 2019, 3:38am UTC](https://forum.ghost.org/t/how-order-get-helper-items-by-field-or-randomly/9526 "2019-10-15T03:38:43Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![luciandavidescu](https://sea1.discourse-cdn.com/flex015/user_avatar/forum.ghost.org/luciandavidescu/32/2270_2.png) [@luciandavidescu](https://forum.ghost.org/u/luciandavidescu)\
**Post date:** [October 15, 2019, 3:38am UTC](https://forum.ghost.org/t/how-order-get-helper-items-by-field-or-randomly/9526/1 "2019-10-15T03:38:44Z")

</div>

If I have a custom list of posts based on slug or id, like this…

```
{{#get "posts" filter="slug:[wednesday,monday,friday,thursday,tuesday]" order="???"}}
{{#foreach posts from="1" limit="5"~}}{{> post}}{{/foreach}}
{{/get}}

```

…what could I do to list them in that exact order instead of chronologically?

---

<div class="post-metadata">

**Author:** ![DavidDarnes](https://sea1.discourse-cdn.com/flex015/user_avatar/forum.ghost.org/daviddarnes/32/3533_2.png) [@DavidDarnes](https://forum.ghost.org/u/DavidDarnes)\
**Post date:** [October 16, 2019, 3:53pm UTC](https://forum.ghost.org/t/how-order-get-helper-items-by-field-or-randomly/9526/2 "2019-10-16T15:53:49Z")

</div>

Hi there. What’s the use case? If you’re outputting the pages in a specific order in the template you might as well use the `#get` helper to get each post so you can control the order 🙂

---

<div class="post-metadata">

**Author:** ![luciandavidescu](https://sea1.discourse-cdn.com/flex015/user_avatar/forum.ghost.org/luciandavidescu/32/2270_2.png) [@luciandavidescu](https://forum.ghost.org/u/luciandavidescu)\
**Post date:** [October 16, 2019, 10:28pm UTC](https://forum.ghost.org/t/how-order-get-helper-items-by-field-or-randomly/9526/3 "2019-10-16T22:28:30Z")

</div>

Well, it’s actually for posts and what I want to do is import the list from an outside source, just checking if it’s feasible at all… I assumed that this sould be one of the basic options

---

<div class="post-metadata">

**Author:** ![DavidDarnes](https://sea1.discourse-cdn.com/flex015/user_avatar/forum.ghost.org/daviddarnes/32/3533_2.png) [@DavidDarnes](https://forum.ghost.org/u/DavidDarnes)\
**Post date:** [October 17, 2019, 8:22am UTC](https://forum.ghost.org/t/how-order-get-helper-items-by-field-or-randomly/9526/4 "2019-10-17T08:22:05Z")

</div>

An outside source you say? Whats the source? I might be in one of our integrations 🙂

> **[Ghost integrations – official apps, plugins & tools](https://ghost.org/integrations/)**
>
> Ghost plugins, tools & apps to integrate withh your Ghost site for automation, analytics, marketing, support and much more! 👉

---

<div class="post-metadata">

**Author:** ![luciandavidescu](https://sea1.discourse-cdn.com/flex015/user_avatar/forum.ghost.org/luciandavidescu/32/2270_2.png) [@luciandavidescu](https://forum.ghost.org/u/luciandavidescu)\
**Post date:** [October 17, 2019, 12:03pm UTC](https://forum.ghost.org/t/how-order-get-helper-items-by-field-or-randomly/9526/5 "2019-10-17T12:03:34Z")

</div>

A custom list of most popular posts

---

<div class="post-metadata">

**Author:** ![DavidDarnes](https://sea1.discourse-cdn.com/flex015/user_avatar/forum.ghost.org/daviddarnes/32/3533_2.png) [@DavidDarnes](https://forum.ghost.org/u/DavidDarnes)\
**Post date:** [October 17, 2019, 3:21pm UTC](https://forum.ghost.org/t/how-order-get-helper-items-by-field-or-randomly/9526/6 "2019-10-17T15:21:14Z")

</div>

Ah so not an outside source? Could you a little more clear on what you are trying to achieve? 🙂

---

<div class="post-metadata">

**Author:** ![luciandavidescu](https://sea1.discourse-cdn.com/flex015/user_avatar/forum.ghost.org/luciandavidescu/32/2270_2.png) [@luciandavidescu](https://forum.ghost.org/u/luciandavidescu)\
**Post date:** [October 17, 2019, 6:28pm UTC](https://forum.ghost.org/t/how-order-get-helper-items-by-field-or-randomly/9526/7 "2019-10-17T18:28:51Z")

</div>

1. Get a list of slugs from Google Analytics API
2. Store them somewhere
3. Fetch them via a custom helper
4. Use the helper to output the slugs in filter=“slug:{{slugs\_custom\_helper}}”  
(I need them fetched via the get helper so I can exclude the posts on subsequent loops)  
But assuming that I just do step 4 manually, I’d still need them sorted in enumeration order to start with.

---

<div class="post-metadata">

**Author:** ![luciandavidescu](https://sea1.discourse-cdn.com/flex015/user_avatar/forum.ghost.org/luciandavidescu/32/2270_2.png) [@luciandavidescu](https://forum.ghost.org/u/luciandavidescu)\
**Post date:** [October 19, 2019, 7:16pm UTC](https://forum.ghost.org/t/how-order-get-helper-items-by-field-or-randomly/9526/8 "2019-10-19T19:16:29Z")

</div>

I thought it might be with something like `order="slug"` or `order="filter:[slug]"` or something  
(@Hannah?)

---

<div class="post-metadata">

**Author:** ![DavidDarnes](https://sea1.discourse-cdn.com/flex015/user_avatar/forum.ghost.org/daviddarnes/32/3533_2.png) [@DavidDarnes](https://forum.ghost.org/u/DavidDarnes)\
**Post date:** [October 23, 2019, 8:08am UTC](https://forum.ghost.org/t/how-order-get-helper-items-by-field-or-randomly/9526/9 "2019-10-23T08:08:40Z")

</div>

I see what you’re trying to do now, order posts by some kind of popularity metric. In that case you could use the Google Analytics API to get a list of the most popular by slug with JavaScript and then use it to order the posts already on the page. What you’re trying to achieve is going to require client-side JavaScript at some point, unless this is a self hosted version of Ghost and you customise the core

---

<div class="post-metadata">

**Author:** ![luciandavidescu](https://sea1.discourse-cdn.com/flex015/user_avatar/forum.ghost.org/luciandavidescu/32/2270_2.png) [@luciandavidescu](https://forum.ghost.org/u/luciandavidescu)\
**Post date:** [October 23, 2019, 9:17am UTC](https://forum.ghost.org/t/how-order-get-helper-items-by-field-or-randomly/9526/10 "2019-10-23T09:17:28Z")

</div>

Well, the whole ideea is to also show posts that aren’t already on the page (maybe something’s still trending one month later - no way to know which in advance).  
Yes, a custom helper should collect the slugs and deliver them to the theme, I suppose.

Later edit: Actually yes, it is possible to finish the job from the frontend, just a bit cumbersome and weird while still needing to edit the core.  
So, the slugs come nicely ordered from the backend, but…  
… the get helper ignores that and reorders them chronologically, so …  
… I have to also output the correct order in something like a HTML attribute or something in order to …  
… use javascript on the frontend to sort them again the way they were in the first place.

---

<div class="post-metadata">

**Author:** ![luciandavidescu](https://sea1.discourse-cdn.com/flex015/user_avatar/forum.ghost.org/luciandavidescu/32/2270_2.png) [@luciandavidescu](https://forum.ghost.org/u/luciandavidescu)\
**Post date:** [October 26, 2019, 4:16pm UTC](https://forum.ghost.org/t/how-order-get-helper-items-by-field-or-randomly/9526/11 "2019-10-26T16:16:54Z")

</div>

No point opening a new thread for this, but on the same note, how about ordering posts randomly - any way to do that as well?

---

<div class="post-metadata">

**Author:** ![DavidDarnes](https://sea1.discourse-cdn.com/flex015/user_avatar/forum.ghost.org/daviddarnes/32/3533_2.png) [@DavidDarnes](https://forum.ghost.org/u/DavidDarnes)\
**Post date:** [October 28, 2019, 10:25am UTC](https://forum.ghost.org/t/how-order-get-helper-items-by-field-or-randomly/9526/12 "2019-10-28T10:25:16Z")

</div>

Hey @luciandavidescu. I’ve noticed you’ve been making a few more complex requests in the forum and I’m wondering if you might get better results from using Ghost as a headless CMS? Have you looked into that at all?  
Using Ghost in this way means you gain more control on how your content is outputted and how your site looks. I realise this doesn’t answer your question directly, but I thought this might be a better route for you and what you’re trying to build 🙂  
Take a look at our write up on the blog: [Ghost on the JAMstack – Future-proof Professional Publishing](https://ghost.org/blog/jamstack/) Additionally you can check out our 11ty starter which uses a similar templating language to our own handlebars templating: [GitHub - TryGhost/eleventy-starter-ghost: A starter template to build websites with Ghost & Eleventy](https://github.com/TryGhost/eleventy-starter-ghost)

If this isn’t right for you then no worries, happy to help you use Ghost handlebars 🙂

---

<div class="post-metadata">

**Author:** ![luciandavidescu](https://sea1.discourse-cdn.com/flex015/user_avatar/forum.ghost.org/luciandavidescu/32/2270_2.png) [@luciandavidescu](https://forum.ghost.org/u/luciandavidescu)\
**Post date:** [October 28, 2019, 4:54pm UTC](https://forum.ghost.org/t/how-order-get-helper-items-by-field-or-randomly/9526/13 "2019-10-28T16:54:25Z")

</div>

Actually I like the simplicity of Ghost and how that constraints me to keep everything simple. However there are 2 or 3 things that I just can’t do without, this beeing one of them (I have a running multiple author blog in WP that I want to migrate to Ghost, I already have the theme replicated and all and am looking to wrap up somehow). If there’s no other way to solve issues, yes, I guess I’ll look into the headless CMS thing and see if it might help.

---

<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:** [October 28, 2019, 5:10pm UTC](https://forum.ghost.org/t/how-order-get-helper-items-by-field-or-randomly/9526/14 "2019-10-28T17:10:56Z")

</div>

> [@luciandavidescu](#):
>
> Use the helper to output the slugs in filter=“slug:{{slugs\_custom\_helper}}”  
> (I need them fetched via the get helper so I can exclude the posts on subsequent loops)

The `{{#get}}` helper is a wrapper around the Content API, as such the filter will be transformed to an API request which ultimately boils down to SQL such as `SELECT * FROM posts WHERE slug IN ('slug3', 'slug1', 'slug2') ORDER BY published_at DESC`.

You can [change the order](https://ghost.org/docs/api/v3/content/#order) but I don’t believe the API will let you specify the type of oder that you want (something like `ORDER BY FIELD(slug, 'slug2', 'slug1', 'slug3')`.

If you need a specific order of posts then I think you’ll need to use multiple `{{get}}`s or resort to manually ordering outside of the `{{get}}` usage - either client-side, or in your custom helper which it sounds like you are trying to use.

> [@luciandavidescu](#):
>
> ordering posts randomly - any way to do that as well?

No, it’s intentional that this is not possible. It’s been explained a number of times on the forum, eg:

> [@Get random post](https://forum.ghost.org/t/get-random-post/7930/6):
>
> Very rarely, there should be as little logic in the core helpers as possible. In this case your custom helper won’t work for the vast majority of setups which use caching in front of a Ghost instance - the first request will get a random entry and that will be cached so every subsequent request will show the same entry. You need to think of Ghost generating static pages rather than dynamic ones, if you need dynamic content for each request then it should be done client-side.

---

<div class="post-metadata">

**Author:** ![luciandavidescu](https://sea1.discourse-cdn.com/flex015/user_avatar/forum.ghost.org/luciandavidescu/32/2270_2.png) [@luciandavidescu](https://forum.ghost.org/u/luciandavidescu)\
**Post date:** [October 28, 2019, 8:48pm UTC](https://forum.ghost.org/t/how-order-get-helper-items-by-field-or-randomly/9526/15 "2019-10-28T20:48:50Z")

</div>

Using multiple gets wouldn’t help as I’m using nesting to exclude already displayed posts on subsequent loops. If there were a way to somehow store post ids and just minus them later on, that would have worked, but apparently it’s not possible either.

L.E. Actually, I might have found a way around - do a general `get` with all the slugs and then, inside it, run separate gets one for each slug. The wrapper get only serves to exclude posts from the other loops, a bit weird and overcomplicated but apparently it works.

---

<div class="post-metadata">

**Author:** ![Jaiden](https://sea1.discourse-cdn.com/flex015/user_avatar/forum.ghost.org/jaiden/32/15449_2.png) [@Jaiden](https://forum.ghost.org/u/Jaiden)\
**Post date:** [October 19, 2021, 9:30am UTC](https://forum.ghost.org/t/how-order-get-helper-items-by-field-or-randomly/9526/16 "2021-10-19T09:30:29Z")

</div>

@luciandavidescu did you end up with a good solution to show the most popular posts?

---

<div class="post-metadata">

**Author:** ![luciandavidescu](https://sea1.discourse-cdn.com/flex015/user_avatar/forum.ghost.org/luciandavidescu/32/2270_2.png) [@luciandavidescu](https://forum.ghost.org/u/luciandavidescu)\
**Post date:** [December 6, 2021, 10:47pm UTC](https://forum.ghost.org/t/how-order-get-helper-items-by-field-or-randomly/9526/17 "2021-12-06T22:47:54Z")

</div>

I had to stick with php at that point / am still thinking on migrating.

For this particular case, I think my approach was that instead of using a list, I would output each slug in a separate file (numbered 0.txt, 1.txt, etc. / updating their contents periodically) and include them in turn, one per #get.

Simply can’t remember how the including would work exactly, but i think it was possible somehow or I would have asked about that as well.

L.E. or maybe not! [Working with static data](https://forum.ghost.org/t/working-with-static-data/8409/)  
(mind you, the actual popularity classification has to be done outside ghost / even with wp, I’m using nginx+lua to effectively count the hits, do the ordering and output the files / all that the cms has to do is import the output)
