Thesis Wordpress Theme brought to you by Astro-Geek:3000

Wordpress: Random Posts Without a Plugin

by Christopher on January 14, 2009

If your Wordpress blog uses a home page or a landing page of some kind, featuring a random post on that page can serve a couple useful purposes. For one, it gives the reader an idea of what they can expect from your blog, especially if you make use of the Excerpt feature. Another benefit is that it helps keep your old archived posts visible. Normally people will only read the latest handful of posts, and ignore the rest. So this helps to open up the reader to even more of your valuable content.

I came across some plugins during my search, but I didn’t try any of them. I was just looking for a simple bit of code that would do the job. At first I didn’t think it would be that difficult; surely Wordpress has a random_post template tag, right? Well, they don’t. Everything I was finding required a plugin of some kind.

I happened across a post at RiteTurnOnly.com that had just what I was looking for. It requires a certain level of PHP knowledge, however – it involves adding a bit of code to The Loop.

A bare bones Loop for a page will look like this:

< ?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

Basically, it keeps checking to see if there are posts to display, depending on criteria. Then it assigns formatting code to each post that meets those criteria.

Well, this random post code that I found simply adds a query_posts function and some variables to this loop. It looks like this:

< ?php query_posts(array('orderby' => 'rand', 'category_name' => SomeCategoryName, 'showposts' => 1)); if (have_posts()) : while (have_posts()) : the_post(); ?>

When putting this into your own page code, all you have to do is change SomeCategoryName to whatever category you want to pull the random posts from. You can also change the showposts variable to however many random posts you want to show.

This will only retrieve the data – you still need to display the data. So after you start the loop, you’ll need to insert some formatting and template tags to get Wordpress to show what you want it to.

Once you’ve got that all figured out, just close the Loop:

< ?php endwhile; else: ?>

< ?php _e('Sorry, no posts matched your criteria.'); ?>

< ?php endif; ?>

This will probably take some tinkering on your part to get the output to display the way you want it to, but for ease of implementation, this Wordpress hack can’t be beat.

Thanks again, Len!

{ 6 trackbacks }

JungleJar Random Posts In Wordpress Without Plugins (Ghost Post Article 1/2)
January 22, 2009 at 4:16 am
JungleJar Tips and Strategies To Keep Past Content Attracting Readers (Ghost Post Article 2/2)
January 28, 2009 at 1:45 am
You are now listed on FAQPAL
December 14, 2009 at 6:06 pm
13 artículos interesantes del Lunes 21 de Dec publicados en otros blogs » hombrelobo, una mente dispersa
December 21, 2009 at 11:03 pm
Blogging Girl » No plugins required: things you can do in WordPress
February 15, 2010 at 8:25 am
Post aleatorios en el index
May 25, 2010 at 10:04 pm

{ 31 comments… read them below or add one }

hakan December 19, 2009 at 12:30 pm

Great tutorial, i will stop to use plugins on my <a href=”http://www.em-re.com”>website </a>. We are waiting more tips from you. Thank you and season greetings to everyone.

Reply

ridwan December 29, 2009 at 11:10 pm

thank u very much.. :)

Reply

CSS Web Gallery January 22, 2010 at 6:38 am

So simple, yet so effective. Of course, problems do occur when you try to use this for one loop over multiple pages using the &paged= option of the query. If anyone has a solution for this please let me know!

Reply

Thomas January 28, 2010 at 1:57 pm

Great Tip!  I used it on collectible-figurines.net to show 8 random posts on home page.  I removed the
‘category_name’ => SomeCategoryName

completely to show posts from any category. But in a random order….thanks to this post! Super quick and easy hack!

Reply

refulez February 16, 2010 at 5:27 pm

is there a way not to repeat the posts

Reply

retro February 19, 2010 at 9:22 pm

cool tut man, im trying to integrate javascript on random posts just for a scroll effect nd its been a bit of a troble, but jus to display random posts this more or less works perfect.

Reply

bayu March 18, 2010 at 1:15 am

but i think it’s not simplier than using plugin..
bayu´s last blog ..Menguntai Kata Awan My ComLuv Profile

Reply

Ryan Cook April 1, 2010 at 4:12 pm

Hey, thanks for the post! It helped, although I shortened the query_posts() code to:
query_posts(‘posts_per_page=1&cat=3&orderby=rand’);
It’s less markup, and also ’showposts’ has been deprecated in favor of ‘posts_per_page’ (http://codex.wordpress.org/Template_Tags/query_posts)

Reply

Greg lam April 21, 2010 at 3:23 pm

Thanks for the tip. It worked exactly as you said. Awesome!

Reply

raja April 21, 2010 at 7:57 pm

orderby=rand was great. It has simply opened doors to some of my old posts. I am able to keep the site re-loaded with different content on refresh.
Thanks for the post.

Reply

Leave a Comment

CommentLuv Enabled

Previous post:

Next post:

-->
How smart is your Theme?  How good is your support? Check out ThesisTheme for WordPress.

About Christopher

I love tech, but don’t have as much time or money to spend on it as I’d like. I get what I need to sustain my Geek vicariously through other sources and pass it along to you.

TwitterFacebookLinkedInDiggStumbleUponTechnoratiDeliciousFriendFeedRobot Skull

Popular Post

Recent Comments

Categories

Archives

Tag Cloud

-->

Twitter - follow me