Movable Type - Related Entries
template recipes | 30 January 2006 |
I’ve long been less than fully happy with the ordinary Related Entries plugin for MovableType. Originally developed by Adam Kalsey, it is now being maintained by Tim Appnel. Tim’s instruction was on the mark — running MT-Related with keywords slows rebuild times and increases overhead enormously.
Naturally, I ran into trouble. Not being a SQL-ey kind of guy, I pottered around in this briar patch for a bit, and only came away bloodied. Of course I had to decide to crank it up a notch and go for the fancy stuff — how to suppress the related entries block if there are none; how to set a threshold limit, etc. etc.
I found Adam’s recipe for suppressing display using MTIfNotEmpty, and for using the score > 5 didn’t work well for me. I kept getting one SQL error after the other. I won’t bore with you with the details, but here it is as I see it (remember, I am not a sql-ey!):
AND SCORE > 5 will never work. It has to be HAVING SCORE > 5
That’s one thing. The second thing is that using the <MTSQLEntries> tag inside the <MTIfNotEmpty> tag invariably returned an error. I had a hellish time trying to escape all the single and double quotes, the various types of brackets and braces, and really got nowhere.
Sometimes, as they say, the simplest answer is the best. Here’s my workaround. It works for me, and should, logically, work for anyone else. I’m not a programmer or a designer, so don’t ask me why — I just did what I thought was logical!
<MTSQLEntries query="SELECT entry_id, MATCH (entry_keywords, entry_title, entry_excerpt) AGAINST ('[MTEntryKeywords encode_php='q'] [MTEntryTitle encode_php='q']') AS `score` FROM mt_entry WHERE MATCH (entry_keywords, entry_title, entry_excerpt) AGAINST ('[MTEntryKeywords encode_php='q'] [MTEntryTitle encode_php='q']') AND entry_id != '[MTEntryID]' AND entry_blog_id = '[MTBlogID]' HAVING `score` >= '2' ORDER BY score DESC LIMIT 0 , 5">
<div class="honor3"><div id="inner">
<a href="<$MTEntryLink$>"><$MTEntryTitle$></a><br />
What happens is really a two-step check. First it checks if you have any keywords or not; if you don’t, nothing displays. If you do, then you have to pass the second threshold, of a score greater than (or greater than or equal to, in the above example) a figure that you determine. Once you do this, it works fine.
Note that the correct usage seems to be HAVING SCORE > whatever (you will have to use >). And, of course, you have to get rid of the silly div tags which are from my own little CSS garden!