sunmat
sunmat

Reputation: 7268

Prevent jekyll for loop from skipping future posts

I have a jekyll website with a blog section that has this:

{% for post in site.posts %}
  ... display post ...
{% endfor %}

This blog section correctly skips posts that are dated in the future.

In another part of my website I want to be able list posts that have a specific "Event" tag, regardless of the date of the post (so even posts in the future should be shown). I have something like this for now:

{% for post in site.posts %}
  {% if post.tag contains "Event" %}
    ... display post ...
  {% endif %}
{% endfor %}

However this loop skips the posts that have a date in the future. How can I prevent this (only for this loop)? Solutions like setting published: true would not work because the blog section would start showing those posts as well.

Upvotes: 0

Views: 131

Answers (1)

juicy-g
juicy-g

Reputation: 496

Unfortunately, the future option is a build configuration option. This means you will have to set it to true, otherwise future posts are skipped from the site.posts collection during build.

Here's a workaround:

After you have set future: true in the _config.yml file, in your blog section, filter the posts like this:

{% assign filtered_posts = site.posts | where_exp: "post", "post.date <= site.time" %}
{% assign sorted_posts = filtered_posts | sort: 'post.date' %}
     
{% for post in sorted_posts %}
  ... display post ...
{% endfor %}

In your other section, you should then be able to list all posts with the Event tag including the future ones using the second code block in your question above.

Upvotes: 1

Related Questions