Sort entries by best match to multiple categories 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?
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,
}) %}