Answers.Body: <p>Have a look at the Similar plugin <a href="https://github.com/aelvan/Similar-Craft" rel="nofollow noreferrer">https://github.com/aelvan/Similar-Craft</a>, it should do exactly what you’re looking for.</p>
  
  <pre><code>{% set baseCriteria = craft.entries({
      order: 'postDate desc',
      limit: 5,
  }) %}
  
  {% set similarEntries = craft.similar.find({
      element: entry,
      context: entry.myCategoriesField,
      criteria: baseCriteria,
  }) %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-08-23T11:24:09.577Z
Answers.Text: Thanks Carl, looks like that'll do it! It doesn't work with eager loading which is a shame, but otherwise looks great.
Answers.UserDisplayName: Frank Harrison
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-08-23T10:17:03.370Z
Answers.LastActivityDate: 2017-08-23T10:17:03.370Z
Answers.UserDisplayName: carlcs
Body: <p>I've got a channel where entries will be tagged with multiple categories. I want to use those categories to show related content for each entry.</p>
  
  <p>So if an entry has cat_a, cat_b and cat_c, I want to pull in any entry that has cat_a, cat_b or cat_c, then order by how many categories they have in common – so an entry matching two of those categories would come before and entry only matching one.</p>
  
  <p>Finally, I want to also sort by date. So I'd first show entries matching all 3 categories (newest first), then entries matching 2 categories (newest first) etc...</p>
  
  <p>Any thoughts on how to do this?</p>
  
Tags: <categories><tags>
Title: Sort entries by best match to multiple categories
ThreadText: # Sort entries by best match to multiple categories
  
  (Asked by: username_0 on 2017-08-23)
  
  I've got a channel where entries will be tagged with multiple categories. I want to use those categories to show related content for each entry.
  
  So if an entry has cat\_a, cat\_b and cat\_c, I want to pull in any entry that has cat\_a, cat\_b or cat\_c, then order by how many categories they have in common – so an entry matching two of those categories would come before and entry only matching one.
  
  Finally, I want to also sort by date. So I'd first show entries matching all 3 categories (newest first), then entries matching 2 categories (newest first) etc...
  
  Any thoughts on how to do this?
  
  **Tags:** categories, tags
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2017-08-23. Score: 3 | Accepted answer
  
  Have a look at the Similar plugin <https://github.com/aelvan/Similar-Craft>, it should do exactly what you’re looking for.
  
  ```
  {% set baseCriteria = craft.entries({
      order: 'postDate desc',
      limit: 5,
  }) %}
  
  {% set similarEntries = craft.similar.find({
      element: entry,
      context: entry.myCategoriesField,
      criteria: baseCriteria,
  }) %}
  
  ```
  
  > **Comments:**
  > **username_0**: Thanks Carl, looks like that'll do it! It doesn't work with eager loading which is a shame, but otherwise looks great.  
  
  ---
  
  