您可以使用令人难以置信的强大pre_get_posts
挂钩来排除帖子。期限在自定义分类
使用您的命名filter
一个自定义分类和premium
术语塞的例子,下面的代码将排除从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',
]);
请确保将此代码片段放置在主题的functions.php
文件中,因为它在WordPress的生命周期中运行得很早。请享用!
来源
2017-08-03 16:52:07
rjb