4

In Craft 3, I’m trying to get the five most recent other posts sharing a category with the current post. I got this far:

{% for relatedPost in craft.entries.relatedTo(entry.newsCategory).limit(5) %}

That gets five posts sharing the current post’s category, including the current post if it happens to be recently posted. How do I exclude the current post from the returned results, without just dropping it from the five so I’m only left with four?

1 Answers1

8

To exclude the current post use the current post's id with a "not" clause in the id query param.

{% for relatedPost in craft.entries.relatedTo(entry.newsCategory).id('not ' ~ entry.id).limit(5).all() %}
a-am
  • 2,857
  • 1
  • 19
  • 25
  • Beauty! Thank you! If you don’t mind another question, where do you recommend people go to learn this sort of thing? – Eric A. Meyer Aug 10 '18 at 20:37
  • 1
    Craft Stack Exchange is the usual place or asking on the Craft Slack channel. https://craftcms.com/community The Craft community is very helpful. – a-am Aug 10 '18 at 20:39