您可以使用非常强大的 pre_get_posts 挂钩来排除自定义分类中带有术语的帖子。
使用名为 filter 的自定义分类示例和 premium 的术语 slug,以下代码将从 RSS 提要中排除这些帖子:
add_action( 'pre_get_posts', function ( $query ) {
if ( is_admin() || ! $query->is_main_query() ) {
return;
}
// Exclude Terms by Slug from RSS Feed
if ( $query->is_feed() ) {
$tax_query = array([
'taxonomy' => 'filter',
'field' => 'slug',
'terms' => [ 'premium' ],
'operator' => 'NOT IN',
]);
$query->set( 'tax_query', $tax_query );
}
}, 11, 1 );
如果您担心分类术语 slug 被更改,您可以指定 term_id 代替 -- 这可能是一个更安全的选择:
$tax_query = array([
'taxonomy' => 'filter',
'field' => 'term_id',
'terms' => [ '1234' ],
'operator' => 'NOT IN',
]);
作为额外的奖励,如果您想排除 所有在自定义分类中具有任何术语的帖子,可以使用以下方法:
$taxonomy = 'filter';
$terms = get_terms([
'taxonomy' => $taxonomy,
'fields' => 'ids',
]);
$tax_query = array([
'taxonomy' => $taxonomy,
'field' => 'term_id',
'terms' => (array) $terms,
'operator' => 'NOT IN',
]);
请务必将此代码 sn-p 放在主题的 functions.php 文件中,因为它在 WordPress 生命周期的早期运行。尽情享受吧!