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?

up vote 3 down vote accepted

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 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 at 20:39

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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