List of Random Posts Widget

I wanted to create a home for my new Random Posts Widget — and so I guess this post is it. I created it with the help of AI and I’m pleased with the result. There are some other WordPress plugins that do the same thing but this one uses modern code1.

It’s pretty straightforward, it just picks between one and 10 random posts and displays them on your WordPress site. That’s it.

Download the List of Random Posts plugin, unzip it, upload it to your wp-content/plugins/ directory and then activate it from your WordPress admin -> plugins page. After it’s live, you’ll find it in your collection of widgets which can be added on your site anywhere widgets are supported.

See the sidebar for the plugin in action.

PHP code after the jump:

<?php
/**
* Plugin Name: List of Random Posts Widget
* Description: Displays random post titles with links. Number of posts can be set between 1 and 10.
* Version: 1.2
* Author: Jeff Milner
* Author URI: https://jeffmilner.com
* Plugin URI: https://jeffmilner.com/index.php/2025/05/11/list-of-random-posts-widget/
*/
class List_Of_Random_Posts_Widget extends WP_Widget {
/**
* Register widget with WordPress.
*/
public function __construct() {
parent::__construct(
'list_of_random_posts_widget', // Base ID
'List of Random Posts Widget', // Name
array('description' => 'Displays random post titles with links.') // Args
);
}
/**
* Front-end display of widget.
*
* @param array $args Widget arguments.
* @param array $instance Saved values from database.
*/
public function widget($args, $instance) {
echo $args['before_widget'];
if (!empty($instance['title'])) {
echo $args['before_title'] . apply_filters('widget_title', $instance['title']) . $args['after_title'];
}
// Get the number of posts to display
$num_posts = (!empty($instance['num_posts'])) ? absint($instance['num_posts']) : 5;
$num_posts = min(10, max(1, $num_posts)); // Ensure between 1 and 10
// Query random posts
$random_posts = new WP_Query(array(
'posts_per_page' => $num_posts, // Number of posts to show
'orderby' => 'rand', // Randomize posts
'post_type' => 'post', // Only fetch standard posts
'post_status' => 'publish', // Show only published posts
'cat' => !empty($instance['category']) ? $instance['category'] : '', // Filter by category
));
if ($random_posts->have_posts()) {
echo '<ul class="random-posts-list">';
while ($random_posts->have_posts()) {
$random_posts->the_post();
echo '<li><a href="' . esc_url(get_permalink()) . '">' . esc_html(get_the_title()) . '</a></li>';
}
echo '</ul>';
wp_reset_postdata();
} else {
echo '<p>No posts found.</p>';
}
echo $args['after_widget'];
}
/**
* Back-end widget form.
*
* @param array $instance Previously saved values from database.
*/
public function form($instance) {
$title = !empty($instance['title']) ? $instance['title'] : 'Random Posts';
$num_posts = !empty($instance['num_posts']) ? absint($instance['num_posts']) : 5;
$num_posts = min(10, max(1, $num_posts)); // Ensure between 1 and 10
?>
<p>
<label for="<?php echo esc_attr($this->get_field_id('title')); ?>">Title:</label>
<input class="widefat" id="<?php echo esc_attr($this->get_field_id('title')); ?>"
name="<?php echo esc_attr($this->get_field_name('title')); ?>"
type="text" value="<?php echo esc_attr($title); ?>">
</p>
<p>
<label for="<?php echo esc_attr($this->get_field_id('num_posts')); ?>">Number of posts (1-10):</label>
<input class="tiny-text" id="<?php echo esc_attr($this->get_field_id('num_posts')); ?>"
name="<?php echo esc_attr($this->get_field_name('num_posts')); ?>"
type="number" min="1" max="10" value="<?php echo esc_attr($num_posts); ?>">
</p>
<label for="<?php echo esc_attr($this->get_field_id('category')); ?>">Category:</label>
<select class="widefat" id="<?php echo esc_attr($this->get_field_id('category')); ?>"
name="<?php echo esc_attr($this->get_field_name('category')); ?>">
<option value="">All Categories</option>
<?php foreach (get_categories() as $cat): ?>
<option value="<?php echo $cat->term_id; ?>" <?php selected($instance['category'], $cat->term_id); ?>>
<?php echo esc_html($cat->name); ?>
</option>
<?php endforeach; ?>
</select>
<label for="<?php echo $this->get_field_id('exclude_cats'); ?>">Exclude Categories:</label>
<input class="widefat" type="text"
name="<?php echo $this->get_field_name('exclude_cats'); ?>"
value="<?php echo esc_attr($instance['exclude_cats']); ?>"
placeholder="e.g., 3,7,12">
<?php
}
/**
* Sanitize widget form values as they are saved.
*
* @param array $new_instance Values just sent to be saved.
* @param array $old_instance Previously saved values from database.
*
* @return array Updated safe values to be saved.
*/
public function update($new_instance, $old_instance) {
$instance = array();
$instance['title'] = (!empty($new_instance['title'])) ? sanitize_text_field($new_instance['title']) : '';
$instance['num_posts'] = (!empty($new_instance['num_posts'])) ? absint($new_instance['num_posts']) : 5;
$instance['num_posts'] = min(10, max(1, $instance['num_posts'])); // Ensure between 1 and 10
return $instance;
}
}
// Register the widget
function register_list_of_random_posts_widget() {
register_widget('List_Of_Random_Posts_Widget');
}
add_action('widgets_init', 'register_list_of_random_posts_widget');

  1. At least according to ChatGPT[↩]

Leave a Reply

Your email address will not be published. Required fields are marked *