archives |  index

Movable Type - Related Entries

A workaround for some problems with Adam Kalsey’s Related Entries recipe (using MT-SQL).

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.

Back to Adam. Found he had a brilliant (hardly surprising) alternative using Brad Choate’s MTSQL plugin (are these guys gods or what?)

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.

Now Adam has a bit on his further comments section on all this, but, somehow, none of it really worked for me. In particular, I couldn’t get his use of Brad’s MTIfEmpty plugin to work at all.

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.

What then?

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!

<MTIfNotEmpty var="EntryKeywords">
<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">
<h3>Related Entries</h3>
<a href="<$MTEntryLink$>"><$MTEntryTitle$></a><br />
</div></div>
</MTSQLEntries>
</MTIfNotEmpty>

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 &gt;). And, of course, you have to get rid of the silly div tags which are from my own little CSS garden!

::© 2006  DaBlogger
January 30, 2006 |  Category: blogging | 

Post a comment

     

You must preview your post, for security reasons.