I upgraded to MT 2.2.1 and MySQL, and I’ve been playing with Brad Choate’s wonderful MT SQL plug-in. Here’s an example of selecting 5 random entries (works in any template):
<ul>
<MTSQLEntries query=”select entry_id from mt_entry order by rand() limit 5″>
<li><a href=”<$MTEntryLink$>”><$MTEntryTitle$></a></li>
</MTSQLEntries>
</ul>
Here’s code to select 5 random entries in the current category (works in Category Archive):
<ul>
<MTSQLEntries query=”select e.entry_id from mt_entry e, mt_placement p, mt_category c where e.entry_id = p.placement_entry_id and p.placement_category_id = c.category_id and c.category_label = ‘<MTArchiveTitle>’ order by rand() limit 5″>
<li><a href=”<$MTEntryLink$>”><$MTEntryTitle$></a></li>
</MTSQLEntries>
</ul>
Here’s code to select the 5 most popular entries, based on the number of trackback pings. I’m not entirely sure about this one, since I had to fake some data to test it and I may have misinterpreted the data model.
<ul>
<MTSQLEntries query=”select trackback_entry_id entry_id, count(*) k from mt_trackback group by entry_id order by k desc limit 5″>
<li><a href=”<$MTEntryLink$>”><$MTEntryTitle$></a></li>
</MTSQLEntries>
</ul>
In all cases, you can change the number after the limit keyword to get more or fewer results.
Did I mention that I really enjoy writing SQL? Expect more of this.

