Home Blog Display Random Posts in WordPress

Display Random Posts in WordPress


Not unlike our articles on rendering a list of future WP posts, and another on listing child pages, the shortcode function on this page utilizes WP’s wp_query() class to display a list of random posts. Not entirely unlike a scheduled post on displaying a reading list, displaying random posts is another means of converting your readership by displaying additional article content. Additionally, providing access to older content showcases valuable content that would otherwise be buried in obscurity.

The Result

To display 5 random posts (the default number) we’ll use the shortcode of [random]. The result is as follows:

While any number of posts greater than 1 is returned in a list, a single post is returned as follows:

Random post from June 26, 2018: Sending Telstra SMS Messages (And The New BeliefMedia Platform)

The purpose of removed formatting is so that the content can be used in one of our text boxes. A sample is as follows:

The textboxes work well in company with single posts because you can return posts relevant to a specific category or tag. For example, to display a random YouTube post (in a YouTube textbox), usage of [bmtextbox type="youtube"][random number="1" tag="69"][/bmtextbox] returns the following (with 69 being the tag ID for ‘youtube’).

Another example: A random Facebook post.

You might choose to use a random post as a means to suggest a ‘recommended’ post. Again, by tag, it’s likely to be relevant. Shortcode of [bmtextbox type="wordpress"][random number="1" tag="66" text="Recommended article from"][/bmtextbox] returns:


+ Related WordPress Post

An example list of random posts with excerpts can be found here.

You may optionally return a post by category or author, and you may also include the description (details below).

WordPress Shortcode

Copy and paste the WordPress function into your theme's functions.php file or, if you sensibly have one installed, your custom functions plugin. You may optionally download and install our plugin from the bottom of of the page.

If you require shortcode to work in a sidebar widget, you'll have to enable the functionality with a filter. If you're using our custom functions plugin, you'll have that feature enabled by default.

Shortcode Attributes

The following list of attributes will alter the behaviour of your list.


For single post, the text is rendered before the data.


To return random posts by specific authors, include their IDs.


To return random posts by category, include the category ID(s).


To return random posts by tag (as demonstrated above), include the tag ID(s).


To include the post excerpt, use description="1"


To remove a specific string of text from the excerpt (handy if you add content to the post), use remove="your text here".


The words attribute is the number of words to include in your excerpt.


The number of posts to return. If more than one post is required, they’ll be returned in a list.


The p attribute is for single random posts. If you’re including the text inline, use default (false). Otherwise, use p="1". This overcomes issues with shortcodes and paragraph breaks.


Default heading style is font-weight: bold;. Alter as required.


Default date style is font-size: 0.9em;. Alter as required.


Default excerpt text style is font-size: 0.9em;. Alter as required.


To avoid costly requests to the WP database, we cache results for 2 hours by default.


  • The function can return posts of any type in an order. However, for the purpose of simplicity we make shortcodes available for each task.


Recommended: Text Boxes With WordPress Shortcode.

Title: BeliefMedia Random Posts
Description: Display random posts in WordPress with shortcode.
Download: Shortcode (V0.2) | WP Plugin (V0.2) | | Plugin Page

Shortt URL for this post: http://shor.tt/1XOh


Please enter your comment!
Please enter your name here