Related posts do not appear to rank tags correctly

I am seeing the same 3 related posts on most every post I make. Tags with 2 or 3 matches are ignored for 1 match items. Is this a known issue?

That section is provided by the theme. Most themes pull anything with a tag that matches. Do you have a lot of posts with duplicate tags? You could edit the theme to use the primary tag instead…

Oh interesting. I thought the theme was just pulling a built in related feature from ghost. I do have lots that have duplicate tags, but I thought i read it would pull more similar tags first, which would be a better result for me. I will contact the theme author.

Yeah, it varies. Most themes that do related posts have that code in partials/related.hbs or partials/related.hbs.

For instance, here’s how Ruby does it:

<div class="related-wrapper gh-outer">
    {{#get "posts" limit="4" filter="tags:[{{post.tags}}]+id:-{{}}" include="tags,authors" as |related|}} excerpted... 

So it excludes the current post, but grabs the first four posts (most recent, since order isn’t specified) with a tag in the group of tags that the current post has. Works great if your tags are mostly orthogonal, badly if all your tags are tagged ‘blog’. Edge and Headline and a number of other themes do the same thing.

Alto does it like this:

{{#get "posts" limit="3" filter="tag:{{primary_tag.slug}}+id:-{{id}}" include="tags" as |related|}}{{#if related}}
<section class="related-posts gh-canvas">
... rest excerpted...

Notice that the #get uses primary tag, so if you have posts where the most important tag is first, you’re likely to get better related posts if you search that way.

If you play with that #get, you can tweak behavior in any theme. You could also add an order parameter, if you don’t want the 3-4 most recent matches to the tag criterion. Or it might make sense to filter by author, for some sites.

1 Like

Ideally, I would like a random selection of posts that match the most tags. My site is a travel blog for my family. I have 3 primary tags: trips, wildlife, automotive. Each continent has its own tag, followed by country, and finally and specific tags for that post. An example of my problem is that I have two posts that have the tags in the following order: Middle East, Qatar, Trips; and Middle East, Jordan, Trips. When I view either of these posts, I only get the newest posts from the Trips tag.

My related posts code in partials is {{#get ‘posts’ limit=‘3’ include=‘tags’ filter=‘tags:[{{post.tags}}]+id:-{{}}’ as |related-posts|}}

Hmm. Tricky. So here’s how you’d have to do it, I think. (Code not tested, almost certainly has a bug/formatting issue.)

{{#get 'tags' filter="tags:[{{post.tags}}]+tags:-[trips,wildlife,automotive,etc]+visibility:public"
limit="1" include="count.posts" order="count.posts asc as |reltags|"
{{#foreach reltags}}
{{#get 'posts' filter="tags:[{{slug}}]-id:{{../../{{id}} limit="3"}}
 -- do some stuff with the related posts here --

So the approach there is to get your most RARE tags on each post, then retrieve other posts with that rarest tag. (I’m assuming that you have no tags applied to fewer than the number of related posts you want. You could instead get your two rarest tags, if your second rarest tag isn’t sometimes super common…)

Thanks, I will try it out this weekend and see how it goes

So, I am not sure how to exactly implement your code into my partials for my theme. Here is the code that I currently have in the page.

{{#get 'posts' limit='3' include='tags' filter='tags:[{{post.tags}}]+id:-{{}}' as |related-posts|}}
  {{#if related-posts}}
    <div class='c-related'>
      <div class='o-grid'>
        <div class='o-grid__col o-grid__col--full'>
          <div class='c-title-bar'>
            <h3 class='c-title-bar__title'>{{t 'You might also like' }}</h3>
        {{> loop }}

I agree that it seems like Ghost should try to match a maximal number of tags when looking for related posts. This feels like it should be another built-in Handlebars templating helper that Ghost provides.

One workaround I’ve used is to simply have fewer tags. For example, if “Trips” and “Middle East” were combined to a single “Trips: Middle East” tag, then related posts all at least to the same part of the world.