
Usually, WordPress is a very simple system to code for, so I was surprised when all I wanted to do was get a random post from a category. There were a couple plugins, but I didn’t want to use them; it’s generally accepted that the fastest way to work with an optimized application like WordPress is to use it’s built-in functions.
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 I created a random post item in my sidebar:
-
Create a Testimonials Category
I created a category for testimonials. In this example, the category ID is 3
-
Create the Query
$test_cat = 3; // Define the category ID for testimonials
$query = "SELECT ID, object_id, term_taxonomy_id FROM $wpdb->posts as p, $wpdb->term_relationships AS t WHERE p.post_status = ‘publish’ && t.term_taxonomy_id = $test_cat && (p.ID = t.object_id) ORDER BY RAND() LIMIT 0,1";
$test_id = $wpdb->get_var($query); // $test_id is now the id of the single post
Let’s break down what is going on in the code above:
- Define the category ID for testimonials (
$test_cat) - Create a query that gets the ID for the post and the category (
SELECT ID, object_id, term_taxonomy_id FROM $wpdb->posts as p, $wpdb->term_relationships AS t) - Make sure that it’s published and not a draft (
WHERE p.post_status = 'publish') - Match up the posts to the defined category (
t.term_taxonomy_id = $test_cat&& (p.ID = t.object_id)) - Request one random result (
ORDER BY RAND() LIMIT 0,1)
- Define the category ID for testimonials (
-
Display the testimonial
<?php query_posts('p='.$test_id); // Call up the random post ?>
<li class="widget">
<h2>Testimonials</h2>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<h3><?php the_title() ?></h3>
<p><?php the_excerpt() ?></p>
<?php endwhile; endif; ?>
</li>
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='.$test_cat).
Katz Web Design, is a 
5 responses so far ↓
Mattias // March 10, 2008 at 10:38 am
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 // March 10, 2008 at 10:59 am
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 havewp_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 // March 17, 2008 at 10:31 am
How would write write the query for WP 2.2?
thanx
Zack Katz // March 17, 2008 at 10:49 am
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.
zayn hamdan // May 31, 2008 at 8:18 am
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
Leave a Comment