Trusted WordPress tutorials, when you need them most.
Beginner’s Guide to WordPress
25 Million+
Websites using our plugins
Years of WordPress experience
WordPress tutorials
by experts

How to Display Random Posts in WordPress (Easy Tutorial)

Editorial Note: We earn a commission from partner links on WPBeginner. Commissions do not affect our editors' opinions or evaluations. Learn more about Editorial Process.

Are you looking for a way to display random posts in WordPress?

Displaying random posts can encourage users to browse through the different articles on your website, resulting in more pageviews and higher user engagement.

In this article, we will show you how to easily display random posts in WordPress.

Displaying random posts in WordPress

Why Display Random Posts in WordPress?

By default, WordPress lists your blog posts in reverse chronological order (from newest to oldest). This allows users to see your latest posts first.

However, most users will not get to see your older articles. For example, if you have been running your WordPress blog for a long time, then your older articles will not be prominently displayed anywhere on the website.

One way to overcome this is by making internal linking a habit. Linking to your older articles in new posts will help users discover them. It will also increase your page views and improve SEO.

Another way around this problem is displaying random posts on your WordPress pages, posts, or sidebar.

This helps users discover content they may not have found otherwise and also allows you to showcase older posts, improving the overall user experience.

Having said that, let’s see how you can easily display random posts in WordPress. We will cover two methods, and you can use the quick links below to jump to the one you want to use:

Method 1: Display Random Posts in WordPress Using WPCode (Recommended)

If you are looking for an easy and customizable way to show random posts in WordPress, then this method is for you.

Many tutorials will tell you to do this by adding code to your theme’s functions.php file. However, the smallest error while typing the code can break your website and make it inaccessible.

That’s why we recommend using WPCode, which is the best WordPress code snippets plugin on the market. It is the easiest and safest way to add custom code to your website.

First, you need to install and activate the WPCode plugin. For detailed instructions, please see our beginners’ guide on how to install a WordPress plugin.

Note: You can also use the free WPCode plugin for this tutorial. However, upgrading to the Pro version will give you access to a cloud library of code snippets, smart conditional logic, and more.

Upon plugin activation, head over to the Code Snippets » + Add Snippet page from the WordPress admin sidebar.

Once you are there, click the ‘Use Snippet’ button under the ‘Add Your Custom Code (New Snippet)’ option.

Add new snippet

This will take you to the ‘Create Custom Snippet’ page, where you can start by typing a name for the code snippet.

This title is only there for your reference and won’t be shown on the website’s front end.

After that, you must choose ‘PHP Snippet’ as the ‘Code Type’ from the dropdown menu on the right side of the screen.

Choose PHP Snippet as the code type for displaying random posts

Next, simply copy and paste the following code into the ‘Code Preview’ box:

function wpb_rand_posts() { 
$args = array(
    'post_type' => 'post',
    'orderby'   => 'rand',
    'posts_per_page' => 5,
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) {
$string .= '<ul>';
    while ( $the_query->have_posts() ) {
        $string .= '<li><a href="'. get_permalink() .'">'. get_the_title() .'</a></li>';
    $string .= '</ul>';
    /* Restore original Post Data */
} else {
$string .= 'no posts found';
return $string;
add_filter('widget_text', 'do_shortcode');

Upon activation, this code will display 5 random posts on your website. You can also change the 'posts_per_page' value to a different number.

Next, scroll down to the ‘Insertion’ section and choose the ‘Auto Insert’ mode.

Even after choosing this mode, you will need to add the [wpb-random-posts] shortcode to your website’s sidebar, page, or post to display random posts.

Keep in mind that this shortcode isn’t a result of the WPCode ‘Shortcode’ feature and is part of the code snippet itself.

Choose an insertion method

Finally, scroll back to the top and toggle the ‘Inactive’ switch to ‘Active’.

After that, simply click the ‘Save Snippet’ button to store your changes.

Save snippet for displaying random posts

Display Random Posts on a WordPress Page or Post

Once you have saved your code snippet, you can display random posts on your website’s page or post using this method.

First, open up a new or existing page/post from the WordPress dashboard.

Next, click the ‘Add Block’ (+) button at the top left corner of the screen to open up the block menu. From here, find and add the Shortcode block to the WordPress page or post.

After that, simply copy and paste the following shortcode into the block:


Add shortcode in a page

Finally, click the ‘Publish’ or ‘Update’ button at the top to save your changes.

Now, you can visit your WordPress website to check out the random posts.

Preview for displaying random posts on a page or post

Display Random Posts in the Sidebar as a Widget

If you are using a classic theme, then this method is for you.

First, you need to visit the Appearance » Widgets page from the WordPress admin sidebar.

Once you are there, just click the ‘Add Block’ (+) button at the top left corner of the screen to open up the block menu.

From here, locate and add the Shortcode block in the ‘Sidebar’ tab. Next, copy and paste the following shortcode into the block:


Add shortcode for displaying random posts in sidebar widget

Finally, click the ‘Update’ button at the top to save your changes.

Now, you can visit your site to check out the random posts displayed in your WordPress sidebar.

Displaying random posts as a widget

Display Random Posts in a Block Theme

If you are using a block-based theme with the full site editor, then this method is for you.

You can start by visiting the Appearance » Editor page from the WordPress admin sidebar. This will launch the WordPress full site editor.

Here, you need to click on the ‘Add Block’ (+) button at the top left corner and add the Shortcode block to your preferred place on the website.

After that, copy and paste the following shortcode into the block:


Add shortcode for random posts in FSE

Finally, click the ‘Save’ button at the top to store your changes.

Now, you can visit your website to see the random list of posts.

Preview for random posts

Method 2: Display Random Posts in the WordPress Sidebar Using the Recent Posts Widget Extended

This method is for you if you want to display random posts in your WordPress sidebar without using any code.

First, you need to install and activate the Recent Posts Widget Extended plugin. For instructions, you can see our tutorial on how to install a WordPress plugin.

Once the plugin has been activated, you can simply display random posts in your WordPress sidebar using a block.

Note: The plugin only works for the widget area in classic WordPress themes. If you are using a block theme, then the plugin’s block won’t be available.

Similarly, the plugin doesn’t allow you to display random posts on a WordPress page or post.

To display random posts in the WordPress sidebar, visit the Appearance » Widgets page from the admin dashboard.

Here, click the ‘Add Block’ (+) button at the top left corner of the screen to open up the block menu. Next, find and add the Recent Posts Extended block to the ‘Sidebar’ tab.

This will open the block’s configuration settings. Here, you can start by typing a title to be shown above your list of random posts.

Add block to widget

Once you have done that, switch to the ‘Posts’ tab in the column on the left.

From here, you can select the post type, post status, and order of the posts that you want to be displayed on your website. You can also choose a custom post type.

Configure settings

Next, scroll down to the ‘Orderby’ dropdown menu and select the ‘Random’ option. If you don’t configure this setting, then the block will only display the most recent posts published on your site.

After that, you can also limit the posts to certain categories by selecting them in the ‘Limit to Category’ section.

Choose random order

You can also configure the settings for thumbnails, excerpts, custom CSS, and more by switching to other tabs in the block.

Finally, click the ‘Update’ button at the top to save your changes. You can now visit your website to view the random posts displayed in the WordPress sidebar.

Preview for displaying random posts

Bonus: Optimize Your Blog Posts For SEO

Other than displaying random posts on your WordPress site, it is also important to optimize each and every post for SEO.

This will improve your website’s search engine rankings and bring more traffic, helping you generate leads.

To optimize your blog posts properly, we recommend using keyword research tools like the WPBeginner Keyword Generator. These tools will allow you to find relevant keywords to use in your content.

WPBeginner keyword generator tool for content updates

Other than that, you can also use SEO writing assistant tools like Semrush to discover LSI and related keywords, change the language tone, and find out the average article length.

You can also use All in One SEO to improve your content quality further. It is the best WordPress SEO plugin on the market that lets you add FAQs and comes with a headline analyzer, AI title generator, article schema, link assistant, and more.

AIOSEO's landing page

All of these features can ultimately help you boost the quality and SEO of your blog posts. For more details, you can see our complete AIOSEO review.

Other tips for increasing blog post ranking include using categories and tags, improving readability, adding a meta description, and using visual content like videos and images in your posts.

To learn more, you can see our beginner’s guide on tips to optimize your blog posts.

We hope this article helped you learn how to display random posts in WordPress. You may also want to see our beginner’s guide on how to choose the best domain registrar for your website and our top picks for the best email marketing services for small businesses.

If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Facebook.

Disclosure: Our content is reader-supported. This means if you click on some of our links, then we may earn a commission. See how WPBeginner is funded, why it matters, and how you can support us. Here's our editorial process.

Editorial Staff

Editorial Staff at WPBeginner is a team of WordPress experts led by Syed Balkhi with over 16 years of experience in WordPress, Web Hosting, eCommerce, SEO, and Marketing. Started in 2009, WPBeginner is now the largest free WordPress resource site in the industry and is often referred to as the Wikipedia for WordPress.

The Ultimate WordPress Toolkit

Get FREE access to our toolkit - a collection of WordPress related products and resources that every professional should have!

Reader Interactions

34 CommentsLeave a Reply

  1. Syed Balkhi says

    Hey WPBeginner readers,
    Did you know you can win exciting prizes by commenting on WPBeginner?
    Every month, our top blog commenters will win HUGE rewards, including premium WordPress plugin licenses and cash prizes.
    You can get more details about the contest from here.
    Start sharing your thoughts below to stand a chance to win!

  2. Jiří Vaněk says

    As I understand, the code is without featured images of posts. Is any options with code to display posts with featured images too?

    • WPBeginner Support says

      You may want to take a look at the latest posts blog in the block editor for that.


  3. Chris Smith says

    My sticky posts always showing up in my random posts. Is there a way to exclude sticky posts from this code?

    • WPBeginner Support says

      To achieve that we would recommend using the plugin from the second method rather than tweaking the code.


  4. Michael says

    Is there an easy way to cache the results for a certain period of time instead of randomizing every time? Maybe like refresh once every 24hrs to reduce lessen the load on DB calls every time?

    • WPBeginner Support says

      For that functionality, you would want to check with the plugin’s support for if it is available.


  5. Marvin Duru says

    Please, i tried adding the shortcode on a specific position on my website not inside the post in PHP but the Random post section did not display but only show my text “[wpb-random-posts]”.

    What should i do?

    • WPBeginner Support says

      When using the block editor you would want to ensure you’re adding it using a shortcode block.


  6. Zeynep says

    Thanks the code works just fine. I changed the first page in my wordpress site to index.html, what do I need to run [wpb-random-posts] on my html page? My English is a bit weak, I hope you understand me.

    • WPBeginner Support says

      You would need the page to be a WordPress page for a WordPress shortcode to work.


  7. Elle says

    How would you go about styling this? I’m getting confused as to how I would control the design of this, i’ve never worked with styling PHP.

    • WPBeginner Support says

      It would depend on the styling you’re wanting to add for what needs to be done. You can add a class in the html code to give you something to target.


  8. Rob Packenham says

    Thanks for the helpful article.

    I have a question — my WordPress site pages include a sidebar with a random post (excerpts) widget.

    A problem is that Google indexes the pages and ‘sees’ the random post excerpts as content for the page. So in Google search, a page can show up as matching a search term only because one of the random post excerpts on the page matched the term (not the main content of page).

    But, when a user clicks on the link in Google search results they will see the page with a different set of random post excerpts in the sidebar — not the post excerpt with matching text. So it looks to them like a bad search.

    Ideally i’d like to exclude the random post excerpts from Google index ie hide them from Google, but that doesn’t look possible (?).

    If you have any suggestions I’d appreciate it! Thanks.

  9. Jun Enriquez says

    Tried this & it destroyed my site, how do I reset this?
    http Error 500
    Please help


  10. Yin says

    Thanks a million for sharing this. I used another plugin for random post that no longer works. Then I find your site and have installed the plugin. It works like a charm.

  11. Lisa says

    Hi, and thanks for this helpful tutorial!
    Can you tell me what would be the piece of code to add to display random posts that are in a specific category? My theme doesn’t display a sidebar so I can’t use the plugin and it already has a random posts display under each article, but posts from all categories are included and displayed then. I tried to go check the theme code and add ‘post_category’ => ‘name-of-the-category’, but it doesn’t work. Any idea?

  12. Raff says

    Hi! I just want to ask what if i want to change random post every week or any given time. For example, i display a set of random post, it will not change until 7days or any given time. Thanks!

  13. Abhimanyu Kashyap says

    Thanks a ton, I was able to display random posts. I changed the no of posts to 1 to display 1 random post. Is there a better way to do it?
    I would also like to find out how I can use a variable instead of post type and enter one where I place the short code eg – [wpb-random-posts(‘post’)].
    How can I achieve this ??

  14. Nate says

    Setting posts to orderly => ‘rand’ is a half-measure. Sure, the post displays ‘randomly’, but like DI SEKITAR mentioned above, what use is displaying one random post if it’s going to be the same random post every time someone visits the site?

  15. smehsan says

    Sorry, But the string was not working and i just make the string global and it works fine now.

    global $string;

  16. Marléne Rose Shaw says

    Thanks for this great tutorial. Is there some way I can get the shortcode and then display the random posts beneath the content in a page instead of the sidebar??

  17. Katherine says


    To exclude the current post add

    ‘post__not_in’ => array(get_the_id())

    to the query.

    So your args will now be:

    $args = array(
    ‘post_type’ => ‘post’,
    ‘orderby’ => ‘rand’,
    ‘posts_per_page’ => 5,
    ‘post__not_in’ => array(get_the_id()),

  18. di sekitar says

    really cool information,
    I tried used plugin that you mention, but i have big question.

    How to make random post different for every visitor or device that visiting my site?

    because i try visit my site using other notebook and smartphone, but the random post from those 2 device is same.

  19. JayKay says


    Advanced Random Posts Widget is a really good plugin, but i have a problem.
    How could you exclude the current/active post when you display a random post ?
    Is it possible to fix this ?

    The problem is the same with the method 2 code.

  20. Gaurav Khurana says

    thanks for this. i was always thinking about this, Since by default the latest post are visible on the home page itself. Thanks for this will definetely use this plugin

Leave A Reply

Thanks for choosing to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and your email address will NOT be published. Please Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.