Aaron Kuzemchak


Random Comments in ExpressionEngine

Posted on January 8, 2009 at 11:24pm / Filed under: ExpressionEngine / 1 comment

Without a doubt, ExpressionEngine is by far my favorite CMS of all time. It's extremely powerful, yet flexible enough to serve a variety of purposes (insert Michael Scott-esque "that's what she said" comment). It never fails to surprise me that just about every time I say "I wish EE had feature X" I discover soon after that a) it does have that feature, or b) I can find a way to make it happen.

Case in point: when I was developing the ExpressionEngine architecture for WaterBuds, I came to the point where the client had requested a page on the site where customers could post testimonials about the product. They also wanted a random testimonial to appear in the sidebar of the homepage.

My first idea in approaching this functionality was to create a new weblog, and use a standalone entry form. The problem, I realized, is that users must have an account in order to use the standalone entry form. Scratch that one. I then figured that I could enable comments on the page itself, allowing users to post testimonials through the comment module (which also allows the site owners to moderate them). It took no time to implement, and worked perfectly.

However…

When it came to displaying a random testimonial on the homepage, it wasn't so straightforward. EE's comment module allows you to set the display order of the comments, but only allows you to set it to one of the following:

  • date
  • email
  • location
  • name
  • url

No option for random. What to do? Well, after that pretty boring story, you're probably thinking "get on with it!" Fine, here's how to do it.

Enter the query module!

Ah yes, the module that nobody ever seems to use (note: this module is not included with EE's Core version)! I too am overly neglectful of this one, but it does tend to save the day quite a bit. A brief study of the comments table in the database gave me all the information for the variables I needed to pull. The following code is where the magic happens:

{exp:query sql="SELECT comment, name FROM exp_comments WHERE (entry_id='X'
  » AND weblog_id='Y' AND status='o') ORDER BY RAND() LIMIT 1"}
<p>{comment}<br />
<strong>{name}</strong></p>
{/exp:query}

That's it! All you need to do is make sure that you put in the correct values for X and Y (these can be found by investigating the database tables, or just using some ingenuity in the control panel). If you were wanting to show a random comment from any of your weblog entries, you would just remove the entry_id statement from the query. And, in case you couldn't figure it out, if you want to show more than one comment, change the number after LIMIT.

See, that was easy. I hope that you find this useful in your own EE projects!

Reader comments

i love Expression Engine, and specially when it is combined with social life. It’s been a while since I did some ExpressionEngine implementation work. I’ve been occupied mostly with design work only and CSS coding.

Posted by Krist Glass on August 10, 2009 at 7:12am

Commenting is not available in this channel entry.