3

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?

6

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() %}
  • 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. craftcms.com/community The Craft community is very helpful. – aran Aug 10 '18 at 20:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.