How to Get Related Posts in Ghost

by
Nistor Cristian 2
in
Tutorials

I'm using the get helper to fetch a collection of posts. I set a limit to 5 posts. You can ajust this limit to whatever you want.

On Ghost docs you can read more about get helper. We are also going to use filter parameter.

Get related posts by tag

{{#get "posts" limit="5" filter="id:-{{id}}+tag:{{primary_tag.slug}}"}}
    {{#foreach posts}}
        <h2><a href="{{url}}">{{title}}</a></h2>
        <p>{{excerpt}}</p>
    {{/foreach}}
{{/get}}

Let's explain the filter parameter in this example:
filter="id:-{{id}}+tag:{{primary_tag.slug}}"
The above filter has 2 rules separated by a +. + is a combinator and represents and.

  • id:-{{id}} - Means that the fetched posts id has to be different from the current post id.
  • tag:{{primary_tag.slug}} - Means that the fetched posts must have set as a tag the primary_tag of the current post.

Now let's try some different examples, that are also based on primary_tag, that have many filter rules.

Get related posts that are featured by tag

{{#get "posts" limit="5" filter="id:-{{id}}+tag:{{primary_tag.slug}}+featured:true"}}

As you can see here we've added featured:true. This returns all posts filtered by tag where the featured property is equal to true.

Get related posts by primary_tag and have set a custom excerpt

{{#get "posts" limit="5" filter="id:-{{id}}+tag:{{primary_tag.slug}}+custom_excerpt:-null"}}

As you can see here we've added custom_excerpt:-null. This returns all posts where the custom_excerpt property is not null.

Get related posts by tag and have set a featured image

{{#get "posts" limit="5" filter="id:-{{id}}+tag:{{primary_tag.slug}}+feature_image:-null"}}

feature_image:-null - returns all posts filtered by tag where the feature_image property is not null.

Get related posts by author

{{#get "posts" limit="5" filter="id:-{{id}}+author:{{author.slug}}"}}
    {{#foreach posts}}
        <h2><a href="{{url}}">{{title}}</a></h2>
        <p>{{excerpt}}</p>
    {{/foreach}}
{{/get}}

The above example in very similar to the previous one. The only thing that changed, obviously, is the filter query.

Let's explain the filter parameter in this example:
filter="id:-{{id}}+author:{{author.slug}}"
The above filter has 2 rules separated by a +.

  • id:-{{id}} - Means that the post id has to be different from the current post id.
  • author:{{author.slug}} - Means that the fetched posts must have the same author as the current post.

Get related posts by author that are published before a given date

{{#get "posts" limit="5" filter="id:-{{id}}+author:{{author.slug}}+published_at:<'2017-10-03'"}}

As you can see here we've added published_at:<'2017-10-03'. This returns all posts where published before 3 Oct 2017.

Get related posts by author and tag

{{#get "posts" limit="5" filter="id:-{{id}}+author:{{author.slug}}+tag:{{primary_tag.slug}}"}}
    {{#foreach posts}}
        <h2><a href="{{url}}">{{title}}</a></h2>
        <p>{{excerpt}}</p>
    {{/foreach}}
{{/get}}

Here, as you can see, we user both author:{{author.slug}} and tag:{{primary_tag.slug}}. This means that we are going to fetch all posts that have the same author with the current post and primary tag of the current post set as a tag.

You can create a lot of combinations and rules based on the fields parameter of a post, tag or author. You might want to read more about fields paramenter to understand better what data you can get for each resource.

Get a quote

Get in touch with us and we will create a custom Ghost theme that fits your branding.

close modal
Submit