# Display posts with a particular variable

**URL:** <https://talk.jekyllrb.com/t/display-posts-with-a-particular-variable/7429>\
**Category:** Help\
**Created:** [June 19, 2022, 8:42am UTC](https://talk.jekyllrb.com/t/display-posts-with-a-particular-variable/7429 "2022-06-19T08:42:30Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![BillRaymond](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.jekyllrb.com/billraymond/32/5963_2.png) [@BillRaymond](https://talk.jekyllrb.com/u/BillRaymond)\
**Post date:** [June 20, 2022, 6:27pm UTC](https://talk.jekyllrb.com/t/display-posts-with-a-particular-variable/7429/2 "2022-06-20T18:27:55Z")

</div>

Jekyll treats front matter like any other piece of data. You can add [pretty much] anything you want there, and it will be available for you to use programmatically.

## 📃 Access within a post/page:

Let’s say you are writing a post about Avatar and want to reference the `film:` front matter in the content of your post. You can use this example to display it within the post:

`/_posts/2022-06-20-all-about-avatar.md`

```auto
---
title: All about Avatar
date: 2022-06-20
film: Avatar (2009)
---
## {{page.title}}

This is my review of {{page.film}}.

```

The result would look something like this:

 ![Screen Shot 2022-06-20 at 11.13.08 AM](https://canada1.discourse-cdn.com/flex030/uploads/jekyllrb/original/2X/3/3cbea0ad8309d26489cd112ce9403b2f1a43a839.png)

Even though that is technically a post, you do **not** use use `post.film`, because, at this point, you are displaying front matter on a page, which is why you should use `page.film`.

## 📑 Add to a list of posts

Let’s say you want a summary page that lists all the posts you made about films. Here is a basic method to do that:

`/index.md`

```auto
---
layout: default
title: "All my posts about films!"
---

## {{page.title}}
<p></p>

{%- for post in site.posts -%}
    Title: <a href="{{post.url |relative_url}}">{{post.title}} / {{post.film}}</a><br>
{%- endfor -%}

```

The result will be a list of _all_ posts on your site, like this:

 ![Screen Shot 2022-06-20 at 11.26.37 AM](https://canada1.discourse-cdn.com/flex030/uploads/jekyllrb/original/2X/3/33c4e9240a62a4ecb5c407de0cbbe2e1aef51306.png)

Notice in the screenshot that there is a post without the title `Introducing my new blog.` That post does not have the `film` YAML front matter, which may be fine for you. However, it is not a list of films, so you will need to modify your code a bit with `assign`, using a `where_exp`, like this:

`/index.md`

```auto
---
layout: default
title: "All my posts about films!"
---

## {{page.title}}
<p></p>

{%- assign posts = site.posts | where_exp: 'post', 'post.film' -%}

{%- for post in posts -%}
    Title: <a href="{{post.url |relative_url}}">{{post.title}} / {{post.film}}</a><br>
{%- endfor -%}

```

Now, the output will look like this:

 ![Screen Shot 2022-06-20 at 10.59.00 AM](https://canada1.discourse-cdn.com/flex030/uploads/jekyllrb/original/2X/1/1cc4e1f27836b89973fd3b36b4c158c58dd2ab41.png)

You might be wondering how that `where_exp` works. It says, “look in site.posts and only give me the results if there is a `film:` front matter item”.

## 💡 Another thought: Jekyll Collections

If you find you will be writing about films, tv shows, books, or other topics, you might want to make your life a little easier and use Jekyll Collections.

With Collections, you can create the equivalent of a `_posts` folder for your films, like `/_films` and tv shows, like `/_tv` and so on.

Here is the official documentation for Jekyll `Collections`:

> **[Collections](https://jekyllrb.com/docs/collections/)**
>
> Collections are a great way to group related content like members of a team or talks at a conference.

Here is a post on how to use collections in a similar fashion you may (or may not) want to use:

> [@Advice for filtering collection with routes](https://talk.jekyllrb.com/t/advice-for-filtering-collection-with-routes/5886/4):
>
> The two most popular ways to accomplish this are with Jekyll Collections or Jekyll Data. I will cover both in this response. The Jekyll Collections approach Here, you create individual files for each of the breweries, and then you can show them one at a time (like a page or a post), or you can iterate through them to create a list. In your \_config.yml file, add the line (anywhere): collections: breweries: Note: If Jekyll is running, even with livereload, you will need to stop and start ag…

---

_[View the full topic](https://talk.jekyllrb.com/t/display-posts-with-a-particular-variable/7429)._
