To display random posts in WordPress, you can use the get_posts()
function along with the orderby
parameter set to 'rand'
. This will retrieve a random set of posts from your database.
Here’s an example code snippet that you can use:
$args = array(
'orderby' => 'rand',
'posts_per_page' => 5, // Change this number to display a different number of random posts
);
$random_posts = get_posts( $args );
foreach ( $random_posts as $post ) {
setup_postdata( $post );
// Display the post title or any other post information you want
the_title();
}
wp_reset_postdata();
This code snippet retrieves 5 random posts from your database and then loops through each post to display its title using the the_title()
function. You can modify the 'posts_per_page'
parameter to change the number of random posts you want to display.
Remember to use this code within the appropriate template file or in a custom WordPress plugin or theme file.
Examples
Example #1: Display Random Posts in a Sidebar Widget
This use case demonstrates how to display a random selection of posts in a sidebar widget on your WordPress site. The code example below creates a custom widget that retrieves a random set of posts and displays their titles and excerpts.
function wpsnippets_random_posts_widget() {
$args = array(
'posts_per_page' => 5,
'orderby' => 'rand',
);
$random_posts = new WP_Query( $args );
if ( $random_posts->have_posts() ) {
echo '<ul>';
while ( $random_posts->have_posts() ) {
$random_posts->the_post();
echo '<li><a href="' . get_permalink() . '">' . get_the_title() . '</a></li>';
echo '<p>' . get_the_excerpt() . '</p>';
}
echo '</ul>';
}
wp_reset_postdata();
}
This code creates a custom widget function that uses WP_Query
to retrieve a random set of posts. The posts_per_page
parameter is set to 5 to limit the number of posts displayed. The orderby
parameter is set to ‘rand’ to randomize the order of the posts. The retrieved posts are then looped through and their titles, permalinks, and excerpts are displayed.
Example #2: Display Random Posts in a Custom Template
This use case demonstrates how to display random posts in a custom template file in WordPress. The code example below can be added to a custom template file to retrieve and display a random set of posts.
$args = array(
'posts_per_page' => 3,
'orderby' => 'rand',
);
$random_posts = new WP_Query( $args );
if ( $random_posts->have_posts() ) {
while ( $random_posts->have_posts() ) {
$random_posts->the_post();
echo '<h2><a href="' . get_permalink() . '">' . get_the_title() . '</a></h2>';
echo '<p>' . get_the_excerpt() . '</p>';
}
}
wp_reset_postdata();
This code uses WP_Query
to retrieve a random set of 3 posts. The posts_per_page
parameter limits the number of posts displayed. The orderby
parameter is set to ‘rand’ to randomize the order of the posts. The retrieved posts are then looped through and their titles, permalinks, and excerpts are displayed.
Example #3: Display Random Posts in a Shortcode
This use case demonstrates how to create a shortcode in WordPress that displays a random set of posts. The code example below creates a custom shortcode that retrieves and displays a random set of posts.
function wpsnippets_random_posts_shortcode( $atts ) {
$atts = shortcode_atts( array(
'posts_per_page' => 5,
), $atts );
$args = array(
'posts_per_page' => $atts['posts_per_page'],
'orderby' => 'rand',
);
$random_posts = new WP_Query( $args );
if ( $random_posts->have_posts() ) {
$output = '<ul>';
while ( $random_posts->have_posts() ) {
$random_posts->the_post();
$output .= '<li><a href="' . get_permalink() . '">' . get_the_title() . '</a></li>';
$output .= '<p>' . get_the_excerpt() . '</p>';
}
$output .= '</ul>';
}
wp_reset_postdata();
return $output;
}
add_shortcode( 'random_posts', 'wpsnippets_random_posts_shortcode' );
This code creates a custom shortcode function that uses WP_Query
to retrieve a random set of posts. The shortcode accepts an optional posts_per_page
attribute to specify the number of posts to display. The retrieved posts are then looped through and their titles, permalinks, and excerpts are stored in the $output
variable. Finally, the $output
variable is returned and displayed wherever the [random_posts]
shortcode is used.