A custom widget with WP_Widget: posts from one category
Classic themes still use widget areas, and since WordPress 5.8 these areas are edited with blocks. Widgets built on the WP_Widget class keep working: they appear in the block widget editor as a “Legacy Widget” block with their own form. For a theme that must support both editors, a WP_Widget class is still the simplest way to ship a configurable sidebar box.
Problem: The sidebar of a classic theme should show the latest posts of a category chosen by the editor, with a title and a number of posts, and the box must work in the Customizer as well as in the block widget editor.
Solution: Extend WP_Widget with widget() for the output, form() for the settings and update() for sanitizing them, register the class on widgets_init, and run a lean WP_Query in widget().
The class
class HA_Category_Posts_Widget extends WP_Widget {
public function __construct() {
parent::__construct( 'ha_category_posts', __( 'Posts from a category', 'textdomain' ), [
'description' => __( 'Latest posts of one category.', 'textdomain' ),
'customize_selective_refresh' => true,
'show_instance_in_rest' => true,
] );
}
public function widget( $args, $instance ) {
$query = new WP_Query( [
'cat' => absint( $instance['category'] ?? 0 ),
'posts_per_page' => absint( $instance['number'] ?? 5 ) ?: 5,
'no_found_rows' => true,
'ignore_sticky_posts' => true,
] );
if ( ! $query->have_posts() ) {
return;
}
echo $args['before_widget'];
$title = apply_filters( 'widget_title', $instance['title'] ?? '', $instance, $this->id_base );
if ( $title ) {
echo $args['before_title'] . esc_html( $title ) . $args['after_title'];
}
echo '<ul>';
while ( $query->have_posts() ) {
$query->the_post();
printf( '<li><a href="%s">%s</a></li>', esc_url( get_permalink() ), esc_html( get_the_title() ) );
}
echo '</ul>';
echo $args['after_widget'];
wp_reset_postdata();
}
public function form( $instance ) {
$title = $instance['title'] ?? '';
$category = absint( $instance['category'] ?? 0 );
$number = absint( $instance['number'] ?? 5 );
?>
<p>
<label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php esc_html_e( 'Title:', 'textdomain' ); ?></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( 'category' ) ); ?>"><?php esc_html_e( 'Category:', 'textdomain' ); ?></label>
<?php
wp_dropdown_categories( [
'id' => $this->get_field_id( 'category' ),
'name' => $this->get_field_name( 'category' ),
'selected' => $category,
'show_option_all' => __( 'All categories', 'textdomain' ),
'class' => 'widefat',
] );
?>
</p>
<p>
<label for="<?php echo esc_attr( $this->get_field_id( 'number' ) ); ?>"><?php esc_html_e( 'Number of posts:', 'textdomain' ); ?></label>
<input class="tiny-text" id="<?php echo esc_attr( $this->get_field_id( 'number' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'number' ) ); ?>" type="number" min="1" max="20" value="<?php echo esc_attr( $number ); ?>">
</p>
<?php
}
public function update( $new_instance, $old_instance ) {
return [
'title' => sanitize_text_field( $new_instance['title'] ?? '' ),
'category' => absint( $new_instance['category'] ?? 0 ),
'number' => min( 20, max( 1, absint( $new_instance['number'] ?? 5 ) ) ),
];
}
}
add_action( 'widgets_init', function () {
register_widget( 'HA_Category_Posts_Widget' );
} );
get_field_id() and get_field_name() build unique IDs and names, so several copies of the widget can live in different sidebars. Whatever update() returns is stored; anything not in the array is dropped. show_instance_in_rest lets the block widget editor preview the widget.
In the block widget editor
The widget shows up in the inserter under its name; WordPress wraps it in a Legacy Widget block and renders the form() inside. To keep the classic widget screen instead, see block-based widgets in WordPress 5.8.
NOTE: Block themes have no widget areas; there, the same output belongs in a block (a Query Loop block with a category filter does it without code). Use WP_Widget for classic themes and for plugins that must support them.