我正在尝试删除没有帖子的术语/分类法。下面是代码,它抛出错误警告:为第二个foreach()提供的参数无效。有什么建议吗?
$terms = get_terms( array(
'taxonomy' => 'post-type',
'hide_empty' => false,
));
$q = new WP_Query($terms);
foreach($q as $term){
foreach($term as $t) {
if($t->count == 0)
wp_delete_term( $t->term_id, 'post-type');
}
}`发布于 2018-05-21 13:01:42
只要改变就好
hide_empty = true例如:
$terms = get_terms( array(
'taxonomy' => 'post-type',
'hide_empty' => true,
));不需要与零计数匹配。它会给出有帖子的结果。不用担心,100%确定它会工作。
发布于 2021-12-16 00:09:39
您的代码中有一个额外的WP_Query。如果您的分类法是post-type,代码可能如下所示。
$terms = get_terms( [
'taxonomy' => 'post-type',
'hide_empty' => false,
] );
foreach ( $terms as $t ) {
if ( 0 === $t->count ) {
wp_delete_term( $t->term_id, 'post-type' );
}
}对于像post_tag这样更常见的分类法,它可能看起来像这样。
$terms = get_terms( [
'taxonomy' => 'post_tag',
'hide_empty' => false,
'update_term_meta_cache' => false,
'hierarchical' => false,
] );
foreach ( $terms as $t ) {
if ( 0 === $t->count ) {
wp_delete_term( $t->term_id, 'post_tag' );
}
}https://stackoverflow.com/questions/50442162
复制相似问题