Exclude categories

#276482
  • Resolved Narul Donghua
    Rank Math free

    Hello Sir,
    i want to know, how I can exclude series from the sitemap. Can I exclude all pages that are in a specific category?. I try to add id in “exclude posts” but it didn’t work out. only that post was removed from the sitemap. I want all posts related to that category should remove using a single id.
    for e.g.-XML sitemaps plugin. that allow remove categories from the sitemap includes all posts related to the categories

Viewing 2 replies - 1 through 2 (of 2 total)
  • Hello,

    Thank you for contacting Rank Math and bringing your concern to our attention. I’m sorry for any inconvenience this issue may have caused you.

    If you want to set noindex to all posts that are assigned to a specific category, then you can add the following code snippet in your theme’s functions.php or rank-math.php file. Just make sure in the code snippet, replace your_category with your category slug.

    /**
     * Filter to set noindex to specific categories
     */
    add_filter( 'rank_math/frontend/robots', function ( $robots ) {
    	$categories = array_map( function ( $category ) {
    		return $category->slug;
    	}, get_the_category() );
    	if ( in_array( 'your_category', $categories ) ) {
    		unset( $robots['index'] );
    		$robots['noindex'] = 'noindex';
    	}
    	return $robots;
    } );

    To find the category slug, you can navigate to Posts > Categories inside your WordPress dashboard.

    category slug

    After applying the code snippet, you can check the source code of your posts assigned to the specific category, noindex meta tag would have been added.

    noindex

    P.S.: Once you’ve added the code snippet to set noindex, any attempt to override a specific post inside the WordPress editor by setting index robots meta tag will not work. The post will continue to show noindex in the front-end until you remove the code snippet.

    I hope this helps you. Please do not hesitate to let us know if you need our assistance with anything else.

    Thank you.

    Hello,

    Glad that helped.

    As you’ve marked the ticket resolved, could you please confirm if there’s anything else that we can assist you with?

    We are here to assist you.

    Thank you.

Viewing 2 replies - 1 through 2 (of 2 total)

You must be logged in to reply to this ticket.