I’m trying to write a new “related posts” widget (for the single post view) for my custom post type, filtering by custom taxonomy terms that the related posts should have in common with the current post. I found this solution.
But what I’d like to add is that the posts are ordered by relevance. I determine the relevancy by the number of taxonomy terms that they have in common: The more taxonomy terms in common, the more relevant.
Example: Current post has tags (1), (2), (3) and (4). A related post with tags (1), (3) and (4) is more relevant than a related post with only the tags (1) and (2).
Also, if possible, I’d like to add special severity/emphasis to one of the custom taxonomy terms (which I assume to be the “main” term of a post).
Example: Current post has tags (1), (2), (3) and (4). (1) is the main term of the post. Posts should only be displayed as related posts, if they have at least this main term in common. A related post with tags (1) and (3) is more relevant than a related post with the tag (1) only. (Optional: A post with the tags (2), (3) and (4) is not considered a related post, or: this post has a lower relevance than any post that has the main term (1) in common.)
Can you please help me with this? Thanks in advance! 🙂
Regards,
René
Sorting your posts by relevance
This will need to add an extra propperty to each post. Than sort the object by this new prperty. The trick is to sort the posts directly in the query object.
This will output the posts ordered by your calculated relevance.
Severity/Emphasis
Calculating a severity or emphasis is just a bunch of
if then else
blocks.Pseudo code:
But how do you want to mark a term as ‘main term’?