Fetch a post by front matter

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

In this documentation

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

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 ?

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

1 Like

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.

1 Like

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.

Set using:

featuredpost: my-post-slug

Read using this:

{% 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:

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

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

Thanks in advance

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

mode: '06'
warranty: 'six years'

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

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

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 ?

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?

I just tested your example,

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

1 Like

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?