# Related posts

**URL:** https://zola.discourse.group/t/related-posts/274
**Category:** Support
**Created:** [December 6, 2019, 8:59am UTC](https://zola.discourse.group/t/related-posts/274 "2019-12-06T08:59:23Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Awea](https://yyz2.discourse-cdn.com/free1/user_avatar/zola.discourse.group/awea/32/123_2.png) [@Awea](https://zola.discourse.group/u/Awea)
#### Post date: [December 6, 2019, 8:59am UTC](https://zola.discourse.group/t/related-posts/274/1 "2019-12-06T08:59:23Z")

</div>

Hi there,  
I’m building a theme for Zola which require a related posts sections. Here is how I made the related posts list at the moment:

```auto
{% if category %}
    {% set related_posts = get_section(path="posts/_index.md")
      | get(key="pages")
      | sort(attribute="date")
      | filter(attribute="taxonomies.categories", value=[category])
      | slice(end=3)
    %}
    <div class="post-items-list columns">
      {% for post in related_posts %}
        {#
          Since we cannot use a negative filter the only solution to remove the current page from the
          related_posts is the following condition
        #}
        {% if post.permalink != page.permalink %}
          {{ macros::post_item(post=post) }}
        {% endif %}
      {% endfor %}
    </div>
  {% endif %}

```

The main problem is that I can’t filter the posts with a negative filter, like: `| filter(attribute="permalink", not_value=page.permalink)` so sometimes `related_posts` contains the current post and I need to filter using an if condition in the display loop. But this solution doesn’t suit me because I end with 2 posts in related posts instead of 3.

Am I missing something or there is no other solution at the moment to achieve that?

---

<div class="post-metadata">

### Author: ![keats](https://avatars.discourse-cdn.com/v4/letter/k/34f0e0/32.png) [@keats](https://zola.discourse.group/u/keats)
#### Post date: [December 6, 2019, 3:07pm UTC](https://zola.discourse.group/t/related-posts/274/2 "2019-12-06T15:07:38Z")

</div>

It seems like it would be easier to do something like:

```auto
{% set related_posts = get_taxonomy(kind="categories", term=post.category) %}

```

and just iterate on that with and skip the current page as you do. But the `term` argument or `get_taxonomy` doesn’t currently exist so you would need to compare the term in the loop.

There are no negative filters in Tera right now 😕 An `exclude` or something could be added to complement `filter`

---

<div class="post-metadata">

### Author: ![Awea](https://yyz2.discourse-cdn.com/free1/user_avatar/zola.discourse.group/awea/32/123_2.png) [@Awea](https://zola.discourse.group/u/Awea)
#### Post date: [December 7, 2019, 10:48am UTC](https://zola.discourse.group/t/related-posts/274/3 "2019-12-07T10:48:55Z")

</div>

Hi @keats,

Thanks for your insight. My question wasn’t complete, I need to limit my `related_posts` to 3 items. So I ended writing something like this:

```auto
{% if category %}
  {% set related_posts = get_taxonomy(kind="categories")
    | get(key="items")
    | filter(attribute="name", value=category)
    | first
    | get(key="pages")
  %}
  {% set_global related_posts_counter = 0 %}

  {% if related_posts | length > 0 %}
    <h2>{{ trans(key="related_posts", lang=lang) }}</h2>

    <div class="post-items-list columns">
      {% for post in related_posts %}
        {#
          Since we cannot use a negative filter the only solution to remove the current page from the
          related_posts and having max 3 related posts is the following condition - @awea 20191207
        #}
        {% if post.permalink != page.permalink and related_posts_counter <= 2 %}
          {{ macros::post_item(post=post) }}
          {% set_global related_posts_counter = related_posts_counter + 1 %}
        {% endif %}
      {% endfor %}
    </div>
  {% endif %}
{% endif %}

```

The term argument in `get_taxonomy` would be great to only fetch the given category with its items ^^

---

<div class="post-metadata">

### Author: ![keats](https://avatars.discourse-cdn.com/v4/letter/k/34f0e0/32.png) [@keats](https://zola.discourse.group/u/keats)
#### Post date: [December 8, 2019, 8:14am UTC](https://zola.discourse.group/t/related-posts/274/4 "2019-12-08T08:14:50Z")

</div>

Yeah it’s quite verbose x)  
I think the `term` argument makes sense to have, I’ll think about it today and create a GH issue for it if I don’t see any issues with it.

---

<div class="post-metadata">

### Author: ![Roman](https://avatars.discourse-cdn.com/v4/letter/r/ed8c4c/32.png) [@Roman](https://zola.discourse.group/u/Roman)
#### Post date: [December 15, 2019, 10:44am UTC](https://zola.discourse.group/t/related-posts/274/5 "2019-12-15T10:44:01Z")

</div>

Hey Awea, Keats,

I’ve tried to repeat that code to get related posts (it’s really valuable thing), but I can’t understand what I supposed to do, if I have two or more tags\categories per article?

---

<div class="post-metadata">

### Author: ![Awea](https://yyz2.discourse-cdn.com/free1/user_avatar/zola.discourse.group/awea/32/123_2.png) [@Awea](https://zola.discourse.group/u/Awea)
#### Post date: [December 15, 2019, 1:41pm UTC](https://zola.discourse.group/t/related-posts/274/6 "2019-12-15T13:41:04Z")

</div>

Hoy Roman,

The theme I’m working on use only **one category per post** and **multiple tags per post**. So I get the first **category** (the first because `taxonomies.categories` is a list) of the current post then I can use it in the `related_posts` above.

```auto
{% if page.taxonomies.categories %}
  {% set category = page.taxonomies.categories | first %}
  <small>{{ category }}</small>
{% endif %}

```

> but I can’t understand what I supposed to do, if I have two or more tags\categories per article?

You can still use the `set category` above. But If you want to work with multiple tags\categories and fetch `related_posts` using those taxonomies you’ll need to rewrite the `set related_posts` part.

Hope it helps.
