2
votes

I'm looking for a way to exclude all posts within a term for a custom taxonomy from my RSS feed. I have a custom taxonomy called "filter" and a term in that taxonomy called "premium" which I'm looking to exclude from my RSS feed.

I've seen numerous places how to exclude posts from a term within a category but have yet to find how I can exclude posts from a term within a a custom taxonomy.

function excludecatfeed($query) {
           if(is_feed()) {
                          $query->set('cat','-1');
                          return $query;
           }
}
add_filter('pre_get_posts', 'excludecatfeed');

This is what I find all the time when searching for this but this is for category, I'm looking to do this same thing but for a custom taxonomy. Thanks

2

2 Answers

2
votes

I was able to figure out how to exclude a Custom Taxonomy from my wordpress feed.

add_filter('pre_get_posts','better_editions_archive');

function better_editions_archive( $query ) {

if ( $query->is_feed() ) {
    $query->set( 'post_type', array( 'post' ) );
    $query->set( 'tax_query', array(
        array(
            'taxonomy' => 'filter',
            'field' => 'slug',
            'terms' => 'premium',
            'operator' => 'NOT IN'
        )
    ) );
}

return $query;
}

In the tax_query you can change what you would like to adjust it to your custom taxonomy. What I"m doing here is I'm excluding all posts in my custom taxonomy called "Filter" and all posts that are in the term "premium".

0
votes

You can use the incredibly powerful pre_get_posts hook to exclude Posts with a Term in a custom Taxonomy.

Using your example of a custom taxonomy named filter and a term slug of premium, the following code would exclude those posts from the RSS feed:

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 );

If you're concerned about the taxonomy term slug being changed, you can specify the term_id instead -- which is a probably a safer choice:

    $tax_query = array([
        'taxonomy' => 'filter',
        'field' => 'term_id',
        'terms' => [ '1234' ],
        'operator' => 'NOT IN',
    ]);

As an added bonus, if you want to exclude all posts having any term in a custom taxonomy, the following could be used:

    $taxonomy = 'filter';

    $terms = get_terms([
        'taxonomy' => $taxonomy,
        'fields' => 'ids',
    ]);

    $tax_query = array([
        'taxonomy' => $taxonomy,
        'field' => 'term_id',
        'terms' => (array) $terms,
        'operator' => 'NOT IN',
    ]);

Be sure to place this code snippet in your theme's functions.php file, since it runs early in WordPress' lifecycle. Enjoy!