Output How Many Posts in Total and How many Paid

Latest version, Dawn theme.

I am trying to understand the best practice of how to get the total number of published posts as well the total number of posts set to “paid” as I am editing the cover.hbs template to include up-sell data to new visitors / potential customers.

The first part, while I am not sure this is best practice in terms of “getting data from the database as in overloading it”, I got the total post count correctly made. I am asking if there are ways to improve this?

{{#get "posts" limit="all"}}

The second part is about counting how many posts that are “paid” only. This part does not give an error, but it does not show the count either. Any ideas on how to get this to work?

{{#get "posts" limit="all"}}
{{#has visibility="paid"}}

Thanks a lot for any help I can get. Still in the learning phase of all of this.

{{#get "posts" limit="1" as |posts pagination|}}
    {{pagination.total}} total posts

{{#get "posts" limit="1" filter="visibility:public" as |posts pagination|}}
    {{pagination.total}} public posts

{{#get "posts" limit="1" filter="visibility:-paid" as |posts pagination|}}
    {{pagination.total}} free posts

{{#get "posts" limit="1" filter="visibility:members" as |posts pagination|}}
    {{pagination.total}} members posts

{{#get "posts" limit="1" filter="visibility:paid" as |posts pagination|}}
    {{pagination.total}} paid-members-only posts

Using limit="1" means you’re not fetching and parsing tons of data from the database and API into memory but you’ll still have access to the pagination object returned from the browse endpoint which can give you the counts.


Thank you @Kevin, I’ll run a few tests on my local server. Appreciate the help.

@Kevin could you please point to resources to better understand this code syntax? Should I look in handlebars or does Ghost has some specific programming language guide or reference?

Handlebars itself is fairly basic in terms of syntax, the short language guide covers everything Introduction | Handlebars

Ghost has it’s own helpers and the data that’s made available for use in templates via the handlebars syntax is very application-specific. All of that is covered in Ghost’s themes docs Ghost Handlebars Themes - Building a custom Ghost theme - Docs

The specific helper used in the examples is the {{get}} helper along with Ghost API’s filter parameter

1 Like

@Kevin Thank you again for this. I added this to my theme (cover.hbs) and now I have up-sell counters that automatically updates as I publish new posts.

@Kevin sorry for spamming you, but I just have to ask. Is there a way, through code injection, adding a total post count in the admin area?

As of now, I have no idea how many posts I have. Looking at Members, there’s a count next to it. Can we add the same for Posts?

No, code injection only works for the front-end.

Adding counts to the posts list is not as simple as it sounds when combined with the sidebar views feature. With the current architecture there are trade-offs that would require considerably bumping the number of API requests that the admin makes to fetch counts, and then added complexity around keeping them in sync and making sure count fetches do not detract from performance of more important API requests. There are also design considerations because it would make the sidebar very busy. It’s something we’ve thought about but there are much more worthwhile things for our tiny development team to be focusing on at the moment :grinning_face_with_smiling_eyes:

Alright. Thanks for taking the time to answer. Now I know.

@Kevin SORRY for the spamming today… Quick question: is there a limit as to how big the export file (json) can be before it needs to be broken down into pieces?

At some point it’s possible for files to become so big they hit node’s built-in memory limits when trying to export/import although that is unlikely to happen when files under a couple of hundred MB of JSON which covers a lot of content. Otherwise there are no specific limits in place. As for when to split files it will depend on the use-case, just remember the export functionality is not intended to be and doesn’t function as a full backup.

@Kevin Thanks for the explanation. I understand that the export is not intended to be a back-up, I just needed to know where the size “limit” is.

I have give or take 1 000 posts whereof some, I am re-writing due to deprecated code and what not. I just wanted to feel safe in knowing that I can export my material without hassle and work on it locally whenever needed.

Again, thanks.