Counting posts on Ghost

Counting posts on Ghost
Photo by Kati Hoehl / Unsplash
tldr: you can use Ghost's content API to quickly grab key stats about your writing output (how many posts, how many posts this year, how many posts by a specific author etc.). Ready to use examples are at the bottom of the post.

Ghost has some great analytics features that tell you how your audience is growing and interacting with your posts. Sometimes though you want to know about the numbers of posts that you've actually made. Here are a few use cases for that which I've heard from Ghost users:

  • I want to make a "wrapped" type post about my year's work
  • I want to make a media pack and need to put some stats together about how often I post
  • I have a large team and I pay people for the number of post contributions they make in a month

Ghost's Content API can be used to grab those numbers. For the purpose of this demo, I'm just going to make API calls directly in the browser, but you could use these ideas to make a dashboard.

🔗 API URL

Your Content API URL is

https://{admin_domain}/ghost/api/content/

The Content API on Ghost(Pro) uses the Ghost subdomain, not the custom domain, but if you happen to use the custom domain in the examples below it will sort itself out 😎

🔑 API key

You can create a new integration for counting posts at https://{admin_domain}/ghost/#/settings/integrations/

And grab its Content API Key:

custom integration editor in Ghost

Alternatively, reuse the key from Ghost's built-in search, which uses the Content API. The content API key is waiting for you via the page source of any of your posts. Search for data-key and the value for this is the Content API key you need!

view source of the homepage of my site, showing the results of a search for "data-key"

The Content API key for this site is ca7e58d44a961762f5403bb308

👋
Ghost(Pro) users on the Starter plan will need to use the key from their site search, as custom integrations are not included on the Starter plan

Using the Content API

You can find full docs for this here. I'm going to show some simple ways to get the data we want. All of these examples run in a web browser.

Count all posts

/posts/?fields=url&limit=1&key={apikey}

Making a call for posts can return a whole load of data that you don’t want (all the content and settings for the first 15 posts by default) fields=url&limit=1 requests just the URL field, and only gets data for 1 post.

Run this in the address bar, and you'll see the first result of your API call plus a count of total posts in the pagination.

This site has 221 posts! I need to go outside and touch grass.

Try it yourself: https://jon.ghost.io/ghost/api/content/posts/?fields=url&limit=1&key=ca7e58d44a961762f5403bb308

To run this on your own site, swap the domain jon.ghost.io so you're using your own Content API URL and change the key value ca7e58d44a961762f5403bb308 to your Content API Key.

Count all posts by a given author

/authors/slug/{slug}/?key={apikey}&include=count.posts

Instead of going through posts, this is going to get data for the author, and count their posts for you.

Here's the demo. At time of writing, I'm the only active author here, so it returns the same number as the all posts count.

https://jon.ghost.io/ghost/api/content/authors/slug/jon/?key=ca7e58d44a961762f5403bb308&include=count.posts

To run this on your own site, swap the domain jon.ghost.io to match your Content API URL, put your author slug in place of jon and change the key value ca7e58d44a961762f5403bb308 to your Content API Key.

Count all posts in a year (an example of filters)

/posts/?fields=url&limit=1&key={apikey}&filter=published_at%3A~%5E%27{year}%27

You can drill down into the number of posts in useful ways with a filter. This example will count all the posts for 2024:

https://jon.ghost.io/ghost/api/content/posts/?fields=url&key=ca7e58d44a961762f5403bb308&limit=1&filter=published_at%3A~%5E%272024%27

Unpacking this for a second, this is the filter without URL encoding:

&filter=published_at:~^'2024'

Which means "filter by the date the post was published, and include any posts whose date starts with '2024'"

Change "2024" to any year to get the count for that year; to get a month, add it to the date string e.g. '2024-12' will tell you how many posts I made in December 2024:

https://jon.ghost.io/ghost/api/content/posts/?fields=url&key=ca7e58d44a961762f5403bb308&limit=1&filter=published_at%3A~%5E%272024-12%27

To run this one for yourself, as before, swap the domain jon.ghost.io to match the one in your Content API URL, change the key value ca7e58d44a961762f5403bb308 to your Content API Key, and put in the year, or year-and-month.

What else?

Filters are pretty cool. You can use these to pinpoint the metrics you need from posts. Whatever you need, you can probably make a filter for it.

You could get all the posts for a tag—for example this will get all posts on my site with the Twitter tag (returns 13):

https://jon.ghost.io/ghost/api/content/posts/?fields=url&key=ca7e58d44a961762f5403bb308&limit=1&filter=tags%3Atwitter

That's not that useful as there are a couple of ways I could surface that in Ghost already. However, combining it with the filter for posts in a year and I can tell you I posted 9 times about Twitter in 2009. What a hopeful time that was.

https://jon.ghost.io/ghost/api/content/posts/?fields=url&key=ca7e58d44a961762f5403bb308&limit=1&filter=tags%3Atwitter%2Bpublished_at%3A~%5E%272009%27

The technique in this post is great for doing a quick bit of self research. If you need something more comprehensive, and ongoing, try using the API to build your own dashboard—and let me know when you've done it, I'd love to see it!