How order get helper items by field or randomly

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}}

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

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

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

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

A custom list of most popular posts

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

  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.

I thought it might be with something like order="slug" or order="filter:[slug]" or something

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

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.

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?

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 :slightly_smiling_face:
Take a look at our write up on the blog: Ghost on the JAMstack – Future-proof Professional Publishing 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

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

1 Like

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.

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

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

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.

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

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
(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)