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 Displaying random posts in WordPress](https://www.wpbeginner.com/wp-content/uploads/2017/01/how-to-display-random-posts-in-wordpress-og.png)
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 Add new snippet](https://www.wpbeginner.com/wp-content/uploads/2023/01/addnewsnippet.png)
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 Choose PHP Snippet as the code type for displaying random posts](https://www.wpbeginner.com/wp-content/uploads/2017/01/php-for-displaying-random-posts-snippet.png)
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() ) {
$the_query->the_post();
$string .= '<li><a href="'. get_permalink() .'">'. get_the_title() .'</a></li>';
}
$string .= '</ul>';
/* Restore original Post Data */
wp_reset_postdata();
} else {
$string .= 'no posts found';
}
return $string;
}
add_shortcode('wpb-random-posts','wpb_rand_posts');
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 Choose an insertion method](https://www.wpbeginner.com/wp-content/uploads/2011/04/choose-an-insertion-method-1.png)
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 Save snippet for displaying random posts](https://www.wpbeginner.com/wp-content/uploads/2017/01/save-snippet-for-displaying-random-posts.png)
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:
[wpb-random-posts]
![Add shortcode in a page Add shortcode in a page](https://www.wpbeginner.com/wp-content/uploads/2017/01/add-shortcode-in-page.png)
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 Preview for displaying random posts on a page or post](https://www.wpbeginner.com/wp-content/uploads/2017/01/display-random-post-on-a-page-preview.jpg)
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:
[wpb-random-posts]
![Add shortcode for displaying random posts in sidebar widget Add shortcode for displaying random posts in sidebar widget](https://www.wpbeginner.com/wp-content/uploads/2017/01/add-shortocode-in-sidebar-widget.png)
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 Displaying random posts as a widget](https://www.wpbeginner.com/wp-content/uploads/2017/01/displaying-post-as-a-widget.jpg)
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:
[wpb-random-posts]
![Add shortcode for random posts in FSE Add shortcode for random posts in FSE](https://www.wpbeginner.com/wp-content/uploads/2017/01/add-shortcode-for-random-post-in-fse-1.png)
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 Preview for random posts](https://www.wpbeginner.com/wp-content/uploads/2017/01/preview-for-random-posts.png)
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 Add block to widget](https://www.wpbeginner.com/wp-content/uploads/2017/01/add-block-to-widget-area.png)
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 Configure settings](https://www.wpbeginner.com/wp-content/uploads/2017/01/configure-settings-for-random-posts.png)
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 Choose random order](https://www.wpbeginner.com/wp-content/uploads/2017/01/choose-random-order.png)
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.
![previewPreview for displaying random posts Preview for displaying random posts](https://www.wpbeginner.com/wp-content/uploads/2017/01/preview.jpg)
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 WPBeginner keyword generator tool for content updates](https://www.wpbeginner.com/wp-content/uploads/2023/10/WPBeginner-keyword-generator-tool-for-content-updates-1-1.png)
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 AIOSEO's landing page](https://www.wpbeginner.com/wp-content/uploads/2023/10/aioseo-landing-page-edited-min.jpg)
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.
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!
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.
Admin
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.
Admin
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.
Admin
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.
Admin
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.
Admin
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.
Admin
Maria Cristhiane Amorim says
Hello! Can I do this in a listing?
WPBeginner Support says
You would need to clarify what you mean by that
Admin
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.
Jun Enriquez says
Tried this & it destroyed my site, how do I reset this?
http Error 500
Please help
Thanks
Jun
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.
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?
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!
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 ??
devender says
thanks shearing this code its very helpful……..
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?
Nate says
Meant to say ‘orderby’ … I was spellcheck’d
smehsan says
Sorry, But the string was not working and i just make the string global and it works fine now.
global $string;
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??
Katherine says
JAYKAY
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()),
);
JAYKAY says
It works fine with this piece of code.
Thank you KATHERINE
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.
JayKay says
Hi,
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.
Harry Stoddart says
Use the “Offset” variable to skip the latest X posts.
Khalil Sillah says
As a beginner what list of matrial would I need to learn wordpress
Ahm Clex says
A PC, WiFi and dedication. That’s all you need to learn anything this days.
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