How to Display a Random Testimonial or Post in WordPress

Set up a testimonials category — no need for a plugin.

There are a couple of plugins designed specifically for testimonials, but I didn’t want to use them; they use their own databases, and don’t keep with WordPress’ simplicity. If possible, the best way to work with WordPress is to use it’s built-in functionality.

I also wanted to have the testimonials as a category in WP, rather than as a separate plugin. This code will work for any type of category, not just a testimonial.

Here’s how to create a random post item in your sidebar:

  1. Create a Testimonials Category

    I created a category for testimonials. In this example, the category ID is 3

  2. Create the Query

    query_posts('showposts=1&cat=3&orderby=rand');

    Let’s break down what is going on in the code above:

    1. Request one result (showposts=1)
    2. Define the category ID for testimonials (cat=3)
    3. Randomize the result (orderby=rand)
  3. Display the testimonial

    <li class="widget">
    <h2>Testimonials</h2>
    
    	<?php
    	if (have_posts()) : while (have_posts()) : the_post();
    	?>
    <h3><?php the_title() ?></h3>
    
    		<?php the_excerpt() ?>
    	<?php endwhile; endif; ?>
    </li>
    
    <?php wp_reset_query(); ?>
    

With the simple code above, you can add testimonials to your WordPress website as a basic category, and have a random testimonial displayed in your sidebar.

To take it one step further, you can create a Testimonials template page, and have it as a static page on your website by simply having query_posts('cat=3).

Use Advanced Category Excluder

You can exclude the testimonials category from your sidebar category widget and many other places on the site without having to tear up your theme and add a lot of custom code. Simply download and install the Advanced Cagtegory Excluder (ACE) plugin for WordPress.

Note: I updated this post to be more relevant and simple. The original post was on 2/21/2008.

This entry was posted in Code and tagged , , , , , , , , , . Bookmark the permalink.
  • Mattias

    Hmm, I cant get it to work.

    All I get it to do is to spew up all entries in the specified category. No randomness and no limit.

    Any ideas?

    /Mattias

  • Zack Katz

    Hi Mattias,

    I had been having that problem as well for a bit, after the initial period where it was working. I’m not sure what made it work again!

    If you’re having issues, test your query using phpMyAdmin and see what results you’re getting back. That’s always a great way to find out exactly what’s going on.

    One thing I’m doing differently is using the great plugin The Excerpt Reloaded (http://guff.szub.net/2005/02/26/the_excerpt-reloaded/), so instead of having the_excerpt(), I have wp_the_excerpt_reloaded().

    Another thing you might want to try is adding the showposts parameter to your query_posts query. So it would be query_posts('p='.$test_id.'showposts=1');

    Let me know how it goes!

  • al

    How would write write the query for WP 2.2?

    thanx

  • Zack Katz

    I’m not familiar with 2.2, but this post looks like it might have a good solution:
    http://wordpress.org/support/topic/123681#post-581065

    What I would do is then create an array of the results and randomize the array with PHP with array_rand.

  • http://kutucape.com zayn hamdan

    There’s a simple to pick up a random post, especially for me who want to display certain random post from my blog (http://kutucape.com). I’m using WP 2.5 and simply using code from here: http://codex.wordpress.org/Template_Tags/query_posts

    I’m using orderby=rand to display random post ;)

  • Joe

    @ zayn your method works and it’s cleaner but for the life of me I can’t get query() to handle the output correctly. I keep getting the same post displayed twice on the home page in two different styles. What I’m really trying to do is create a section for a specific category that will display one random post from that category but I just keep getting one post dispayed twice on the page

    this is all I have before the loop query_posts(‘cat=7&showposts=1&orderby=rand’);

  • Pingback: Links for 8th October 2008 | Velcro City Tourist Board

  • http://www.DanielCondurachi.com Daniel Condurachi

    @zayn’s method is very good. But you can not apply it without thinking. @joe, you said that you do query_posts(’cat=7&showposts=1&orderby=rand’); The order in the query_posts matters. So what you do in there is 1st you choose all the posts from a certain category (7) then from all those post you choose just one by the default order which is by date, so you get the last post. After this you do random form one post which will always be 1

    Thank you @zayn for your hint. I used your idea of just 2 word instead of a script with many many lines. A BIG thank you!

  • Mayur
  • http://www.naseerahmad.com/ Naseer Ahmad

    how can i get the post of the open category…

    like i have category “Services” and wants to open the related post at the bottom of post.

    e.g. i have posts, design service, development service, etc… and when i select the category tab… other post will display at bottom…

    How can i get this… plz reply ungently…
    on my email webmaster [at] naseerahmad [dot] com

    warm regards,
    naseer

  • http://www.seodenver.com Zack Katz

    @Naseer – you could do something like this: $test_cat = get_the_category();. The problem is that if get_the_category() returns more than one, it will be in an array. Read WordPress Codex: get_the_category for more code and information.

  • http://thisismyurl.com thisismyurl

    Thanks Zack, it’s a great piece of code.

  • http://www.richwp.com Felix

    Hope I didn’t miss something. That should do it:

    Felix

  • Jason

    Or Try 'rand', 'category_name' => SomeCategoryName, 'showposts' => 1)); if (have_posts()) : while (have_posts()) : the_post(); ?>

  • http://www.speaklouder.co.uk George Dickson

    many thanks for this info. implementing your information as we speak!

  • http://highpointseo.com Chris Gragg

    Good post on this topic. I sphunn this one today.
    Best Regards, Chris

  • Cindy

    I’d love to use this.  I just don’t know where to put the query code.  Then I’m assuming the testimonial code goes in the text widget?  Thanks for your help.

  • http://bubbleletters.gretah.com Bubble letters

    can not get it work…

  • http://www.keooweewer.com booker

    Thank you very much for any help!

  • Pingback: WordPressハッカーズ

  • Venkatesh

    How to chose multiple categories ? Can anyone kindly help me…

    • Justine

      List all category ids, separated by a comma. Eg:
      query_posts(‘showposts=1&cat=3,4,7,18&orderby=rand’);

  • http://www.divydovy.com David Lockie

    Sweet. Works a treat, thanks.

  • http://www.vul.kr www.vul.kr

    Oderby=rand is to slow in query,you should use this:
    http://www.vul.kr/how-to-display-rand-post-article-in-wordpress

  • Steve McKebab

    Hi there, this looks simple enough but im pretty new, please could someone tell me where to insert the code. Found this because I was pulling my hair out looking for a decent testimonial plugin, doing it this method seems a better solution. Thanks in advance

  • http://www.tuning-links.com Tuning

    This was really helpful for me. I have never read about php but needed to mod my WordPress theme to show some results in random order.
    Thanks!

  • Sandeepnegi82

    gfdfdf

  • Matthew

    This was a big help. I’ve now got random testimonials displaying on my website’s inner pages.
    Check it out! (It’s still under construction
    http://www.cleverlyinvite.com/quick-contact