Noindex posts displayed in Sitemap.

#810251
  • Resolved Webnow Team
    Rank Math free

    Hello,
    We have used rank_math/frontend/robots hook to apply noindex for some old posts from
    year 2017 to 2022.
    But the issue is they are still showing in sitemap.
    If we apply noindex from Admin Post Edit screen, then sitemap does not displaying those, but we cannot apply noindex from admin post edit screen.

    rank_math/frontend/robots hook adding noindex correctly in frontend, but why sitemap showing those posts.
    We have tried rank_math/sitemap/include_noindex hook also to return false, but it also does not help.
    We have also removed canonical tag, but that also does not help.

    Please provide solution ASAP if possible

    Here is my code

    add_filter( 'rank_math/frontend/robots', function( $robots ) {
      global $post;
    
      // Check if the post belongs to the "haberler" category
      if ( has_category( 'haberler', $post->ID ) ) {
        // Get the publication date of the post
        $post_date = get_the_date( 'Y-m-d', $post->ID );
    
        // Convert the publication date to a DateTime object
        $publication_date = new DateTime( $post_date );
    
        // Define the start and end dates for the range (2017-01-01 to 2022-12-31)
        $start_date = new DateTime( '2017-01-01' );
        $end_date   = new DateTime( '2022-12-31' );
    
        // Check if the publication date is within the specified range
        if ( $publication_date >= $start_date && $publication_date <= $end_date ) {
          unset( $robots['index'] );
    			$robots['noindex'] = 'noindex';
        }
      }
      return $robots;
    });
Viewing 3 replies - 1 through 3 (of 3 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.

    Please note that the filter sets the posts as Noindex in the front end. They will still be set to Index in the backend and will be included in the sitemap.

    That said, you can actually use the rank_math/sitemap/entry filter to forcefully remove the URLs from your sitemap.

    You can learn more about the filter here: https://rankmath.com/kb/filters-hooks-api-developer/#filter-sitemap-item

    Here’s an example code you can use and customize to remove URLs:

    add_filter( 'rank_math/sitemap/entry', function( $url, $type, $object ){
    	// List of URLs to exclude
    	$exclude = array(
    		'https://yourwebsite.com/post-1/',
    		'https://yourwebsite.com/post-2/'
    	);
    
    	// Check if the URL is in the exclude list
    	if ( in_array( $url['loc'], $exclude ) ) {
    		// Return false to skip this URL
    		return false;
    	}
    
    	// Return the original URL otherwise
    	return $url;
    }, 10, 3 );

    Hope that helps.

    Ok thanks, it will be not easy for us to add posts URL from year 2017 – 2022. And we cannot update all URLs admin settings as well manually, that is why we were using hook for noindex, but sitemap is depend on DB setting. In that case there is no use of noindex using filter hook.
    Thank you for your response.

    Hello,

    We understand that this couldn’t be achieved by the filter we have shared.

    However, if you have any other concerns, please don’t hesitate to contact us anytime to assist you further.

    Looking forward to helping you.

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

The ticket ‘Noindex posts displayed in Sitemap.’ is closed to new replies.