Filtering authors with zero posts

Hello,

I am building an authors list page and I need to get a list of authors with their post counts. I’m using the #get function helper with an include for the post counts. But the variable count.posts which I use to output post counts in the template doesn’t seem to be accessible using the #has helper function. Here’s my example:

{{#get "authors" limit="all" include='count.posts' order='count.posts desc'}}
    {{#foreach authors}}
        {{#count}}
            {{#has posts="0"}} ZERO POSTS {{/has}}
        {{/count}}

        Name: {{ name }}<br>
        Posts: {{count.posts}}
    {{/foreach}}
{{/get}}

Is there alternatively a way to filter authors with zero posts using an actual filter with the #get function helper?

Thanks for your help. I’m developing against ghost:3.33-alpine.

You could try adding a filter to the {{get}} helper…

{{#get "authors" limit="all" include="count.posts" order="count.posts desc" filter="count.posts:>0"}}
1 Like

Seems like filter="count.posts:<1" would filter all authors with zero posts, but it filters all authors.

{{#get "authors" limit="all" include='count.posts' order='count.posts desc' filter="count.posts:<1"}}

Correct me if I’m wrong, but isn’t an Author a user who has published at least 1 post? Or does the get helper allow fetching Users?

Also, I think filtering by count.posts is still not supported in the get helper :grinning_face_with_smiling_eyes:

I was under that impression too, but it appears that the account I used to create the blog with is listed as an author with 0 posts for all of the sites I’ve migrated to ghost.

So there is no need to filter by 0 posts. The problem seems to be that ghost is adding the ghost owner user to the list of authors.

Good point!

Interesting, sounds like a bug if that’s reproducible!

Would something like this work in the meantime?

{{#get "authors" limit="all" include='count.posts' order='count.posts desc'}}
    {{#foreach authors}}
        {{#if count.posts}}
            Name: {{ name }}<br>
            Posts: {{count.posts}}
        {{else}}
            ZERO POSTS
        {{/if}}
    {{/foreach}}
{{/get}}

This is exactly how I ended up solving it. I came back to update in case anyone else runs into this issue. The solution was {{#if count.posts}}. Thanks for the help!

Are you by chance using the v2 API? In the package.json of the theme, is there a line that says

    "engines": {
        "ghost-api": "v2"
    },

or something similar? I think the change happened in the v3 API which if I’m not mistaken is the theme API version

I’m using v3

"engines": {
        "ghost": ">=3.0.0",
        "ghost-api": "v3"
    },