I was curious if was possible to display related posts by multiple tags.
The site I am working on has about 5 tags per post. Most posts have 1 or 2 tags in common. The related posts I’d like to show have 3-5 tags in common. I would link the site, but this is all locally developed with mamp.
So, I’d like the related posts to function by looking for posts with the most number of tags in common and display them in descending order.
Let’s say I display 3 related posts: relatedpost1 would have 4 tags in common, relatedpost2 would have 3 in common and relatedpost3 would have 1 in common.
Is it even possible to do this?
Right now I am messing around with two ways of displaying the posts but they arent functioning as I’d like:
The first method (code here: http://codepad.org/K9UGSYuc) just shows posts with ANY tags in common.
The second method (code here:http://codepad.org/ZGrzMXIE) just shows posts with the first tag in common.
I’m either getting pretty random posts displaying (since most of my posts have at least 1 tag in common) or (for some posts) getting no related posts (since their common tags are tag 4 or 5).
Any help would be greatly appreciated.
Getting posts with any common tags is a good start. I cannot conceive of how to write an ORDER BY clause to get the order you want. There may be a way, but I’m pretty weak with mySQL. As an alternative, you could use PHP’s usort() to sort the array returned by your current query. Just come up with a callback function that returns the difference in common tag count for the two posts passed to it by usort().
I actually got this done and by me, I mean a programmer I know did it for me.
I’m not sure exactly how it works (from a coding stand point)
But basically it go through all the tags and lists related posts in order of tags in common. I only show 3 related posts, so a good example would be relatedpost1 has 100% tags in commong, related posts2 has 80% in common and relatedpost3 has 20%.
If more than one post meet the criteria it then, also, shuffles the posts, so that the related posts aren’t the same every single click.
It work fantastic; but I am not a php coder so I don’t even know where/how he implemented it.
Thanks for the reply.
- The topic ‘Related Posts by Multiple Tags’ is closed to new replies.