WordPress Related Posts with a Twist

Today I’m mostly coding WordPress related posts functionality without using a plugin. The twist is that the related posts need to be prioritised and ordered by the number of matching tags.