Get our posts emailed to you with our monthly newsletter, subscribe here.

There are many plugins available that allow WordPress users to add customized lists of posts in WordPress, however its really easy to do this with a few simple lines of code. Doing this eliminates the excessive use of WordPress plugins.

Some WordPress plugins I belive are essential, for example Akismet, but some plugins you can really do without. It is good practise to de-activate un-used plugins and delete their files on your server and their entries in your DB.

If a plugin only consists of a few lines of code, then you can do without it, saving you unnecessary script and css file includes.

A simple example is customizing and adding additional lists of posts in your blog, this can be done using the built in WordPress function get_posts:

[php]<?php get_posts( $args ); ?>[/php]

This function allows you to create multiple loops separate from the main Loop and pass a variety of paremters to the funcion, allwoing you to a highly customizable post list.

Display Random Posts

To display a list of random posts on your WordPress blog we can use the get_posts function like so:

[php] <ul>
$args = array( ‘numberposts’ => 5, ‘orderby’ => ‘rand’, ‘post_status’ => ‘publish’, ‘offset’ => 1);
$rand_posts = get_posts( $args );
foreach( $rand_posts as $post ) : ?>
<li><h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2><p><?php the_excerpt(); ?>
<?php endforeach; ?>

Parameters In Use

Lets take a look at what parameters we have passed to get_posts, controlling what is displayed is vital, you do not want readers seeing un-published posts, or posts already displayed on the index page.

‘numberposts’ => 5

This limits the amount of posts that will be displayed.

‘orderby’ => ‘rand’

The most important part of displaying a set of random posts, using the MySQL RAND() function will produce a random generated list of posts.

‘post_status’ => ‘publish’

To ensure that posts that are either draft, deleted or pending review from being displayed it is good practise to specify that only posts that are published be displayed!

‘offset’ => 1

I added this parameter to offset the first post from being included in the list.

Using this method you be able to display a random list of WordPress posts without the use of a plugin, just like the right hand list on the DW index page.

Posted by David Martin

David Martin is a WordPress developer & entrepreneur from the UK. He owns DesignWoop, CardDsgn and WhoDesignToday. Catch up with David on Twitter.