如果有人在看这个问题,我设法找到了一个解决方案。1、切换到复选框而不是多选,并使用“选中”而不是“选中”;2、使用了tax\\u查询和其他一些查询修改。
该代码并不完美,但目前仍有效。
用于显示CPT中按单个或多个类别排序的最后10个条目的工作小部件代码:
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\' );
});