I am making a custom D7 module which has a block that lists related nodes of the current node you're viewing. The relation is determined by Taxonomy terms (from 1 vocab). The complication is that the related nodes need to be ordered by how many tags are matched in descending order.
How can you order nodes by matching tags? I have an idea of how to select nodes with matching tags, but have no clue as to how to order these by the number of matching tags. Any suggestions? Do it in SQL or in PHP?
Are there any modules that implement such functionality?
Simple case study for clarity
I have 2 content types: basic_page, article
Both of these content types have a same term reference field: field_tags
When someone opens a
basic_page node that has been tagged with Tag A, Tag B and Tag C, I want to show
article nodes that have been tagged with Tag A, Tag B and Tag C. But these
article nodes need to be ordered by 'relevancy'. For example: the
article nodes with all 3 tags matched must be at the top, and
article nodes with 2 matching tags comes next, then the ones with 1 matching tag.