• Resolved Waqas

    (@waqaslone)


    Hi,

    I’m trying to exclude posts manually from XML sitemap using ‘wpseo_sitemap_entry” filter but so far have no luck.

    This is my code:

    function sitemap_exclude_post( $url, $type, $post) {
    		if($post->ID == 6298 ) {
    			return false;
    		} 
    
    	return $url;
    }
    add_filter( 'wpseo_sitemap_entry', 'sitemap_exclude_post', 1, 3 );

    Can someone plz have a look and see what I’m doing wrong?

Viewing 2 replies - 1 through 2 (of 2 total)
  • Sa?a

    (@stodorovic)

    Hi @waqaslone

    It should work. Anyway, I’m adding additional checking for type:

    function sitemap_exclude_post( $url, $type, $post) {
    	if ( ($type === 'post') &&  $post->ID == 6298 ) {
    		return false;
    	} 
    
    	return $url;
    }
    add_filter( 'wpseo_sitemap_entry', 'sitemap_exclude_post', 20, 3 );
    

    Also, try to use higher priority (eg. 20) because your filter should be last.

    Sasa

    Thread Starter Waqas

    (@waqaslone)

    Hi @stodorovic

    Thank you for looking into it. It appears that the sitemap is not updating for some reason.

    Initially, I tried your code and it didn’t work but then after hitting the “save changes” button inside Yoast’s dashboard Exclude Posts section it worked.

    To test further, I changed the post ID to a different post’s ID but again the changes didn’t update on the post-sitemap.xml until I used the same method (via dashboard).

    Regards,
    Waqas

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Excluding posts from XML Sitemap using Filter’ is closed to new replies.