Similar Posts: Version 1.10 Recent Posts: WordPress Plugin

Random Posts: WordPress Plugin

Print Version October 26th, 2006


13th January, 2007

Random Posts Version 2.0.0 beta is now available. Largely rewritten, it includes some new options and many more possible styles of display. Version 2 now has its own page where future developments will be documented.

Comments are now closed for this post but can be added to the new page.

Version 1.04 just fixes two bugs. The Posts plugin were not working if more than one was installed! Also some users were getting odd characters appearing where they shouldn’t.
Version 1.03 allows some parameters (like ‘before_title’) to be blank, or to be more complex, e.g., 'before_title=

. Also allows trimming an excerpt so it ends with a word or a sentence and not in mid-word. NB excerpt_length is now counted in characters and not words as previously.
Version 1.02 fixes a bug with the option to show static pages.
Version 1.01 fixes bugs with default settings when Similar Posts is not also installed.
Adds the skip parameter to ignore a number of posts at the head of the list.



Simply displays a list of posts chosen at random from your blog. Good for getting visitors to explore the depths of your site.

This plugin was designed as a cousin to Similar Posts. It will work fine on its own but if Similar Posts is installed Random Posts will take its settings from the Similar Posts options page.


  1. Download the latest version of Random Posts.
  2. Upload the whole plugin folder (Random_Posts) to your /wp-content/plugins/ directory.
  3. Go to your Admin|Plugins page and activate Random Posts.
  4. Putat the place in your WP loop where you want the list of random posts to appear. By default the plugin wraps each post with
  5. and
  6. but that can be changed.
  7. The behaviour of the plugin can be adjusted in two ways. If Similar Posts is installed you can use its options page. Otherwise, a query-style parameter does the trick (see below).


Random Posts was inspired by Scott Reilly’s excellent Customizable Post Listings plugin.

Under the Hood

The way the list of random posts is displayed can be set from the Options|Similar Posts page (if it is installed). You can exclude certain categories of post, for example, or change the code that comes before and after the link.

These general options can be overridden in specific cases by passing a query-style parameter, e.g.:

<!--?php random_posts('limit=10'); ?-->
lists 10 random posts
<!--?php random_posts('none_text=sorry&#038;show_static=false'); ?-->
lists the default number of posts, excluding static pages, and specifies what to display if there are none

If you do not specify an option its value is taken from the options page.
This means you can use the template tag in different ways in different places.

The full list of parameters is as follows (with the default value in parentheses):

maximum number of posts to show (5)
how many posts to skip before listing (0)
include static pages (false)
include password-protected posts (false)
comma separated list of categories to exclude (by ID) (9999, the default means none)
comma separated list of authors to exclude (by ID) (9999, the default means none)
what to show if no posts match–can be plain text or a permalink
what to show before a link ()
what to show after a link()

remove the first instance of ‘before_title’ (false)
include a snippet of the post after the link (false)
how long an excerpt should be (50 characters)
‘char’, the default, does nothing, ‘word’ trims the excerpt to the last full word, and ‘sent’ to the full sentence. If the excerpt would be trimmed to nothing no trimming is applied.
add ‘ …’ after the excerpt
what to show before an excerpt ()
what to show after an excerpt ()


If you try this plugin leave a comment here to let me know how you get on.

Entry Filed under: Programming,WP Plugins


  • 1. inaz  |  October 26th, 2006 at 3:44 pm

    Instead of excluding categories I would really like to see included categories.

  • 2. Rob  |  October 26th, 2006 at 3:52 pm

    Inaz: I’ll look into it.

  • 3. Rob  |  October 27th, 2006 at 8:52 am

    inaz: Can you tell me the advantage of doing it that way around? I ask because there are certain problems with coding inclusion rather than exclusion.

  • 4. Lior C  |  November 14th, 2006 at 1:59 am

    Line 153

    $output .= $p['before_title'] .'<a href="'. $permalink .'" rel="bookmark" title="'.__('Permanent Link: ') . $title . '">' . $title . '</a>' . $p['after_title'];

    Should be

    $output .= stripslashes($p['before_title']) .'<a href="'. $permalink .'" rel="bookmark" title="'.__('Permanent Link: ') . $title . '">' . $title . '</a>' . $p['after_title'];

    so that a call to the function like so:
    <?php random_posts('limit=5&before_title=<li class="page_item">'); ?>

    doesn’t yield an output of:
     <li class=\"page_item\">

    All in all, great plugin. Thanks!

  • 5. Rob  |  November 14th, 2006 at 11:33 am

    Lior C: Thanks for the report. I’m fixing it. The PHP function I was using, parse_str, wasn’t designed to handle arguments with extra ‘=’ in them and was throwing in many extra slashes. I’ve written my own parsing routine which seems to work well … but I’ll test a bit more before releasing it.

  • 6. Calypso  |  December 10th, 2006 at 6:18 pm

    Thanks a lot for this piece of work, the only random-post-plugin which is working for me! I’m so happy 😉

  • 7. Rob  |  December 11th, 2006 at 11:06 am

    Calypso: I’m glad it’s meeting a need.

  • 8. Michael Sky  |  December 17th, 2006 at 8:30 pm

    Hi, great plugin. Just have one question: is there any way to get it to display the Excerpt before the Title?

    Or, any way to display the full post rather than the excerpt?


  • 9. Rob  |  December 17th, 2006 at 9:30 pm

    Michael: Both will be possible in the next version — I’m just waiting for some time to make it happen…

  • 10. Rob  |  January 13th, 2007 at 11:07 am


October 2006
« Sep   Nov »

Related Reading