Where_exp issue with date formats

I am building out a new Jekyll site. I have some pages where I want to show the latest posts and others where I want to show future posts.

In the _config.yml file, I set:

unpublished: true

I have a GitHub action that builds my site at 12:30 am each day. That means “today” will always be the latest date since the latest Jekyll build.

For the page that only displays the latest posts (not future-dated), I wrote this code:

{%- assign todays-date = 'now' | date: '%s' -%}
{%- assign posts = site.posts | where_exp: 'post', 'post.date <= todays-date' | date: '%s' -%}
{%- for post in posts -%}
    <p>Title: {{post.title}}
{%- endfor -%}

The where_exp fails with the following error:

Error: Liquid error (line 10): comparison of Time with String failed

I believe that I am properly using the date format for today’s date and can assume that the post date is a string.

How can I (a) convert the string within an where_exp or (b) easily list out all the posts less than or equal to today’s date?

Thanks!

Possibly you could use site.time to compare with post.date? Both should be Time type.

post.date is returning as a string, which is why I’m struggling with this.

@chuckhoupt Sorry, I mis-read your comment and went back to it and you are correct! That solution worked. For reference, here is my new code:

    {%- assign posts = site.posts | where_exp: 'post', 'post.date <= site.time' -%}

Thanks again!

1 Like