# Fetch a post by front matter

**URL:** <https://talk.jekyllrb.com/t/fetch-a-post-by-front-matter/4336>\
**Category:** Help\
**Created:** [May 1, 2020, 4:09pm UTC](https://talk.jekyllrb.com/t/fetch-a-post-by-front-matter/4336 "2020-05-01T16:09:40Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![keshinishudan](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.jekyllrb.com/keshinishudan/32/2485_2.png) [@keshinishudan](https://talk.jekyllrb.com/u/keshinishudan)\
**Post date:** [May 1, 2020, 4:09pm UTC](https://talk.jekyllrb.com/t/fetch-a-post-by-front-matter/4336/1 "2020-05-01T16:09:40Z")

</div>

Wondering if there is an easy way to fetch a post and its contents from elsewhere in jekyll.

I have been using the following loop to fetch and display contents of a post from another page and it works

```
{% for post in site.posts %}
{% if post.frontmattervariable == "1" %}
<h2><a href="{{ post.url | prepend: site.baseurl }}">{{ post.title }}</a></h2>  
<p>{{ post.content }}</p>
{% endif %}
{% endfor %}

```

Question is there a **simple way** to fetch a post from id or say the front matter variable, am i missing the trick here ?

**I need to fetch the content of only one post and how can i do so without having to loop through all**

---

<div class="post-metadata">

**Author:** ![michaelbach](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.jekyllrb.com/michaelbach/32/2151_2.png) [@michaelbach](https://talk.jekyllrb.com/u/michaelbach)\
**Post date:** [May 2, 2020, 5:30am UTC](https://talk.jekyllrb.com/t/fetch-a-post-by-front-matter/4336/2 "2020-05-02T05:30:11Z")

</div>

In this documentation

> **[Tags Filters](https://jekyllrb.com/docs/liquid/tags/)**
>
> All of the standard Liquid tags are supported. Jekyll has a few built in tags to help you build your site. You can also create your own tags using plugins.

  
It says:  
«If you want to include a link to a post on your site, the `post_url` tag will generate the correct permalink URL for the post you specify.  
`{% post_url 2010-07-21-name-of-post %}` »  
Does that help in your case?  
Best, Michael

---

<div class="post-metadata">

**Author:** ![keshinishudan](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.jekyllrb.com/keshinishudan/32/2485_2.png) [@keshinishudan](https://talk.jekyllrb.com/u/keshinishudan)\
**Post date:** [May 2, 2020, 11:07am UTC](https://talk.jekyllrb.com/t/fetch-a-post-by-front-matter/4336/3 "2020-05-02T11:07:39Z")

</div>

Hey @michaelbach  
Thanks for coming forward to help

But how can i **fetch the contents** of that post ?

The tag filter just outputs a _string_ that carries the post url, but I would need the contents of that particular post referenced through the url just something like

> set post = post.url  
> {{ post.content }}

Is it possible to in the first place ?

---

<div class="post-metadata">

**Author:** ![michaelbach](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.jekyllrb.com/michaelbach/32/2151_2.png) [@michaelbach](https://talk.jekyllrb.com/u/michaelbach)\
**Post date:** [May 2, 2020, 6:37pm UTC](https://talk.jekyllrb.com/t/fetch-a-post-by-front-matter/4336/4 "2020-05-02T18:37:24Z")

</div>

Ah, I misunderstood your question. Out of my depth here. Woud `include` help?

---

<div class="post-metadata">

**Author:** ![rdyar](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.jekyllrb.com/rdyar/32/710_2.png) [@rdyar](https://talk.jekyllrb.com/u/rdyar)\
**Post date:** [May 2, 2020, 7:43pm UTC](https://talk.jekyllrb.com/t/fetch-a-post-by-front-matter/4336/5 "2020-05-02T19:43:51Z")

</div>

I don’t think you can do it as you are basically asking it to loop thru all posts and find this particular one, which is a loop.

Is this just one specific post? or multiple times you want to do something similar? if it is just one then maybe the include is a good idea. Maybe even put the loop in the include and that would only run once maybe? you could pass in the frontmattervariable to the include.

---

<div class="post-metadata">

**Author:** ![keshinishudan](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.jekyllrb.com/keshinishudan/32/2485_2.png) [@keshinishudan](https://talk.jekyllrb.com/u/keshinishudan)\
**Post date:** [May 3, 2020, 2:59pm UTC](https://talk.jekyllrb.com/t/fetch-a-post-by-front-matter/4336/6 "2020-05-03T14:59:48Z")

</div>

Thanks for letting me know, I guess then including the loop as a library and calling via **include** might be the only option in hand.

Also I am getting to know about **collections** in jekyll, which may be a close alternative.

---

<div class="post-metadata">

**Author:** ![jhvanderschee](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.jekyllrb.com/jhvanderschee/32/583_2.png) [@jhvanderschee](https://talk.jekyllrb.com/u/jhvanderschee)\
**Post date:** [May 4, 2020, 8:24am UTC](https://talk.jekyllrb.com/t/fetch-a-post-by-front-matter/4336/7 "2020-05-04T08:24:05Z")

</div>

Set using:

```auto
---
featuredpost: my-post-slug
---

```

Read using this:

```auto
{% assign selectedpost = site.posts | where:'slug',page.featuredpost %}
<h2><a href="{{ selectedpost[0].url | prepend: site.baseurl }}">{{ selectedpost[0].title }}</a></h2>  
<p>{{ selectedpost[0].content }}</p>

```

I am not sure about `slug`, you might want to try `path` if it does not work.

If everything fails use:

```auto
{% assign selectedpost = site.posts | where_exp:"item","item.url contains page.featuredpost" %}

```

---

<div class="post-metadata">

**Author:** ![keshinishudan](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.jekyllrb.com/keshinishudan/32/2485_2.png) [@keshinishudan](https://talk.jekyllrb.com/u/keshinishudan)\
**Post date:** [May 4, 2020, 7:02pm UTC](https://talk.jekyllrb.com/t/fetch-a-post-by-front-matter/4336/8 "2020-05-04T19:02:55Z")

</div>

Can you please explain with this example

> post 1 - Front matter  
> model: “05”  
> warranty: “five years”

> post 2 - Front matter  
> mode: “06”  
> warranty: “six years”

Help me design a constructor that fetchs the **post 2** through mode == “06”  
And hence print the warranty period of the particular model…  
Preferably through your afore mentioned method.(i.e) using  
_where\_exp:_

Thanks in advance

---

<div class="post-metadata">

**Author:** ![jhvanderschee](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.jekyllrb.com/jhvanderschee/32/583_2.png) [@jhvanderschee](https://talk.jekyllrb.com/u/jhvanderschee)\
**Post date:** [May 4, 2020, 7:10pm UTC](https://talk.jekyllrb.com/t/fetch-a-post-by-front-matter/4336/9 "2020-05-04T19:10:25Z")

</div>

Let us assume you have a post with this front matter:

```auto
---
mode: '06'
warranty: 'six years'
---

```

You can access that post and its front matter from anywhere in your Jekyll website by using:

```auto
{% assign selectedpost = site.posts | where: 'mode','06' %}
{{ selectedpost[0].warranty }}

```

---

<div class="post-metadata">

**Author:** ![keshinishudan](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.jekyllrb.com/keshinishudan/32/2485_2.png) [@keshinishudan](https://talk.jekyllrb.com/u/keshinishudan)\
**Post date:** [May 4, 2020, 7:33pm UTC](https://talk.jekyllrb.com/t/fetch-a-post-by-front-matter/4336/10 "2020-05-04T19:33:13Z")

</div>

Almost there… Wow

I see that this method works **only** when i set

> | where: “title”, “nameofthepost”

And it works as expected  
But the thing is it works _only_ for variable **title:** and any other custom front matter variable fails to return output (shows no error though)

Should i be adding something to initialize the front matter variable in config.yml ?

---

<div class="post-metadata">

**Author:** ![jhvanderschee](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.jekyllrb.com/jhvanderschee/32/583_2.png) [@jhvanderschee](https://talk.jekyllrb.com/u/jhvanderschee)\
**Post date:** [May 4, 2020, 9:04pm UTC](https://talk.jekyllrb.com/t/fetch-a-post-by-front-matter/4336/11 "2020-05-04T21:04:43Z")

</div>

You must have made a typo. It is working on my machine with other variables. I have tested it. Did you treat the numbers as strings perhaps?

---

<div class="post-metadata">

**Author:** ![keshinishudan](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.jekyllrb.com/keshinishudan/32/2485_2.png) [@keshinishudan](https://talk.jekyllrb.com/u/keshinishudan)\
**Post date:** [May 5, 2020, 1:07pm UTC](https://talk.jekyllrb.com/t/fetch-a-post-by-front-matter/4336/12 "2020-05-05T13:07:24Z")

</div>

I just tested your example,

```auto
---
mode: '06'
warranty: 'six years'
---

```

unable to fetch **warranty** from mode.  
However the otherway works, that is to say I can fetch **mode** through warranty from your code …

Clearly this has something to do with integers and strings mismatch.  
Was that you were referring to when you were asking

> Did you treat the numbers as strings perhaps

---

<div class="post-metadata">

**Author:** ![jhvanderschee](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.jekyllrb.com/jhvanderschee/32/583_2.png) [@jhvanderschee](https://talk.jekyllrb.com/u/jhvanderschee)\
**Post date:** [May 5, 2020, 1:32pm UTC](https://talk.jekyllrb.com/t/fetch-a-post-by-front-matter/4336/13 "2020-05-05T13:32:57Z")

</div>

Yes indeed. Great… so now you know what the problem is. Can you try some extra/better escaping of the numbers? Or can you try to enforce ‘string’ treatment by adding a letter in front of the number?
