-
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; });
The ticket ‘Noindex posts displayed in Sitemap.’ is closed to new replies.