Home / Admin / Exclude All Posts of a Category from News Sitemap
Duplicate Snippet

Embed Snippet on Your Site

Exclude All Posts of a Category from News Sitemap

This snippet can be used to exclude All Posts of a Category from News Sitemap.

Note: Replace "136" in the $category_id with the ID of the category you want to exclude.

<10
Code Preview
php
<?php
add_filter( 'aioseo_sitemap_exclude_posts', 'aioseo_sitemap_filter_excluded_posts', 10, 2 );
function aioseo_sitemap_filter_excluded_posts( $ids, $type ) {
    if ( 'news' === $type ) {
        $category_id = 136; 
        
        $posts_in_category = get_posts(array(
            'category' => $category_id,
            'numberposts' => -1,
            'fields' => 'ids'
        ));
        
        $ids = array_merge($ids, $posts_in_category);
    }
    return $ids;
}

Comments

Add a Comment