If someone is looking at this question, I managed to find a solution.
1. Switched to a checkbox instead of a multi-select and used “checked” instead of “selected”;
2. Used tax_query and some other query modifications.
The code is not perfect but it works for now.
Working Widget Code for Displaying the last 10 entries in a CPT sorted by single or multiple categories:
class CPT_Widget extends WP_Widget
{
public function __construct()
{
parent::__construct(
'widget_category_posts',
_x( 'CPT Widget', 'CPT Widget' ),
[ 'description' => __( 'Display the latest entries in your custom post type sorted by single or multiple categories.' ) ]
);
$this->alt_option_name="widget_category_posts";
add_action( 'save_post', [$this, 'flush_widget_cache'] );
add_action( 'deleted_post', [$this, 'flush_widget_cache'] );
add_action( 'switch_theme', [$this, 'flush_widget_cache'] );
}
public function widget( $args, $instance ) {
$cache = [];
if ( ! $this->is_preview() ) {
$cache = wp_cache_get( 'widget_cat_posts', 'widget' );
}
if ( ! is_array( $cache ) ) {
$cache = [];
}
if ( ! isset( $args['widget_id'] ) ) {
$args['widget_id'] = $this->id;
}
if ( isset( $cache[ $args['widget_id'] ] ) ) {
echo $cache[ $args['widget_id'] ];
return;
}
ob_start();
$title = ( ! empty( $instance['title'] ) ) ? $instance['title'] : __( 'Category Posts' );
$title = apply_filters( 'widget_title', $title, $instance, $this->id_base );
$number = ( ! empty( $instance['number'] ) ) ? absint( $instance['number'] ) : 5;
if ( ! $number ) {
$number = 5;
}
$cat_id = $instance['categories'];
if( true === $random ) {
$query_args = [
'posts_per_page' => 10,
'post_type' => 'yourcustomposttype',
'orderby' => 'rand',
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'term_id',
'terms' => $cat_id,
),
),
];
}else{
$query_args = [
'posts_per_page' => 10,
'post_type' => 'yourcustomposttype',
'orderby' => 'rand',
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'term_id',
'terms' => $cat_id,
),
),
];
}
$q = new WP_Query( $query_args );
if( $q->have_posts() ) {
echo $args['before_widget'];
if ( $title ) {
echo $args['before_title'] . $title . $args['after_title'];
}
while( $q->have_posts() ) {
$q->the_post(); ?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<header class="entry-header">
<?php the_title( '<h1 class="entry-title"><a href="' . esc_url( get_permalink() ) . '" rel="bookmark">', '</a></h1>' ); ?>
</header><!-- .entry-header -->
<div class="post-thumbnail">
<?php the_post_thumbnail(); ?>
</div><!--/.post-thumbnail-->
<div class="entry-summary">
<?php the_excerpt(); ?>
</div><!-- .entry-summary -->
<?php } ?>
</article><!-- #post-## -->
<?php
wp_reset_postdata();
}
echo $args['after_widget'];
if ( ! $this->is_preview() ) {
$cache[ $args['widget_id'] ] = ob_get_flush();
wp_cache_set( 'widget_cat_posts', $cache, 'widget' );
} else {
ob_end_flush();
}
}
public function update($a, $b) {
return array(
'title' => isset($a['title']) ? strip_tags($a['title']) : $b['title'],
'categories' => isset($a['categories']) ? array_filter(array_map(function($id) { return intval($id); }, (array) $a['categories'])) : (array) $b['title']
);
}
public function flush_widget_cache()
{
wp_cache_delete('widget_cat_posts', 'widget');
}
public function form($instance) {
$title = isset($instance['title']) ? $instance['title'] : '';
$categories = isset($instance['categories']) ? $instance['categories'] : array();
?>
<p>
<label for="<?php echo $this->get_field_id('title') ?>">
<?php _e( 'Title:' ) ?>
</label>
<input class="widefat"
id="<?php echo $this->get_field_id('title') ?>"
name="<?php echo $this->get_field_name('title') ?>"
value="<?php echo $title ?>" />
</p>
<p>Categories</p>
<ul>
<?php foreach (\get_categories() as $category): ?>
<li>
<label>
<input type="checkbox"
class="checkbox"
name="<?php echo $this->get_field_name('categories') ?>[]"
value="<?php echo $category->cat_ID ?>"
<?php checked(in_array($category->cat_ID, $categories)) ?> />
<?php echo $category->name ?>
</label>
</li>
<?php endforeach ?>
</ul>
<?php
}
}
add_action( 'widgets_init', function ()
{
register_widget( 'CPT_Widget' );
});