Count posts in current year?

Hi folks,

I’m looking to add an includes into my posts where it counts the amount of published posts in the current year and outputs it to a simple string like:

“This is post number X that I have published so far this year.”

I assume I can use something like

{{ site.posts | size }}

But I don’t know how to filter that to only count posts in the current year.

Thanks in advance!

Have a look at getting the date on a post and date for today.
Then compare the year.

Hint: the date method can get you just the year.
Second hint: You don’t actually need to use size. If you use where_expr or for/if to filter to posts in the current year, then if there are zero posts to show them none will be shown

I don’t want to give a full answer, so you can go through the research process.

Searches for Jekyll syntax on Jekyll docs or a look.

If you still don’t come right, post the code that you found.

Are you sure you want to filter current year only? Your blog page will go from 100 posts to 0 posts on 1 Jan.

Why not filter to the last 3 or 10 posts? Already a common pattern on Jekyll themes.

Or group posts by year. There was a recent forum discussion covering this. Showing the year as a heading before the post preview, but skipping the heading if the year is the same as the next item


## 2021

- post title
- post title

## 2020

- post title

## 2019


Also don’t know if a reader cares about the distinction of grouping posts into years. I prefer to look at a stream of all posts and see the date for a given post for context. Having a category like JavaScript as a heading or as a page of only those posts is more useful than year.

Thanks for the replies. Maybe I wasn’t clear in my original post. I’m not trying to have this liquid filter spit out links to post, I simply want a count of posts for the current year.

So I will have a sentence at the end of the post that says “This post number X that I have published this year.”

Where “X” is the output from the count filter.

I’ve decided to change my approach (thanks for the cheat sheet @MichaelCurrin it has been really helpful). What I want to do works, but it now spits out a lot of warnings at build - which I want to avoid, obviously.

So I have 100days21.html which I’m using in an includes statement. That file has the following contents:

<p class="hundred-days">This is post number <span class="hundred-days-number">{{ page.100days21 }}</span> in my 2021 <a target="blank" href="">100 Days To Offload</a> challenge.</p>

I also have 100days21 in my front matter for the posts I want to include this file on. So, in my posts layout I’ve added the following:

{% if page.100days21 %}
  {% include 100days21.html %}
{% endif %}

This correctly parses my front matter and only includes the 100days21.html file on the post with the relevant front matter. However, it spits out a tonne of warnings like this on build:

Liquid Warning: Liquid syntax error (line 10): Expected id but found number in "page.100days21" in C:/Users/Kev/GitHub/

I’m sure there’s a more elegant solution to this that will prevent these warnings, I just don’t know what it is. Any help would be greatly appreciated.


Make sure your frontmatter variable and includes file don’t start with numbers.

Use _100 instead of 100.

In programming usually this is invalid variable

100days = "abc"

Sorry I missed the part about count.

You can use filters.

Convert array of hashes to array of strings.

Then convert to array of years. Then filter to current year.

{% current_year = ... %}
{% assign post_years = site.posts | map: "date"
  | date: "%Y" %}
{% assign post_years_this_year = post_years | where: current_year %}
{{ post_years_this_year.size }}

Use today or now or something and date filter to get current year set. Cheatsheets will help

Make sure your frontmatter variable and includes file don’t start with numbers.

That worked perfectly - thank you so much. And thank you for not just giving me the answer, but rather the resources to learn about it myself so I could get most of the way there on my own.

1 Like

Awesome to hear.

You’re welcome.