• Resolved James

    (@jamesbcox1980)


    I’d like to know how to use this filter in order to remove certain pages from my sitemap. I’ve tried adding this function to my theme functions:

    //Remove specific pages from sitemap
    function remove_pages_sitemaps( $post_names ) {
    	$post_names[] = 'contact';
    	return $post_names;
    }
    add_filter( 'jetpack_sitemap_skip_post', 'remove_pages_sitemaps' );

    But all of my pages except for the homepage were removed from my sitemap.

    https://www.ads-software.com/plugins/jetpack/

Viewing 12 replies - 1 through 12 (of 12 total)
  • Plugin Author Jeremy Herve

    (@jeherve)

    Jetpack Mechanic ??

    The jetpack_sitemap_skip_post filter accepts 2 parameters: $skip (true when the post should be skipped, false when it shouldn’t), and $post, an object with information about the post.

    You’ll consequently need to build your code snippet like so:

    function jeherve_exclude_post_sitemap( $skip, $post ) {
            if ( '2146' ==  $post->ID ) {
                    $skip = true;
            } else {
                    $skip = false;
            }
    
            return $skip;
    }
    add_filter( 'jetpack_sitemap_skip_post', 'jeherve_exclude_post_sitemap', 10, 2 );

    You will of course need to replace 2146 by the ID of your post.

    I hope this helps.

    Thread Starter James

    (@jamesbcox1980)

    Thank you very much! Works perfectly. I plan to use my custom fields to exclude certain posts and this will make it far easier to implement.

    Pardon my ignorance, I see the POST ID exclusion, but how about excluding pages? James asked about page exclusion?

    Plus how to to multiple page/post exclusion?

    Thanks mucho.

    • This reply was modified 8 years, 2 months ago by AL Guevara.
    Plugin Author Jeremy Herve

    (@jeherve)

    Jetpack Mechanic ??

    @albabes You can enter any post ID there; if the post ID happens to match a Page, that page will be excluded.

    how to to multiple page/post exclusion?

    You can edit my example above to add as many post IDs as you’d like!

    Hi Jeremy. It It did not work.
    I’m using the “code-snippets” plugin by the way to insert into functions file.
    https://www.ads-software.com/plugins/code-snippets/

    See screenshots below showing page ID=72 in code, but still showing up in the sitemap. Any suggestions?

    the Jetpack xml sitemap
    the Jetpack xml sitemap

    Also, adding your code to the child themes fucntions file caused a siter error.

    ERROR:
    Fatal error: Cannot redeclare jeherve_exclude_post_sitemap() (previously declared in /home/content/a2pewpnas01_data01/05/3958905/html/wp-content/plugins/code-snippets/php/snippet-ops.php(384) : eval()’d code:1) in /home/content/a2pewpnas01_data01/05/3958905/html/wp-content/themes/generatepress-child-theme-01/functions.php on line 37

    The code Im using.

    function jeherve_exclude_post_sitemap( $skip, $post ) {
            if ( '72' ==  $post->ID ) {
                    $skip = true;
            } else {
                    $skip = false;
            }
    
            return $skip;
    }
    add_filter( 'jetpack_sitemap_skip_post', 'jeherve_exclude_post_sitemap', 10, 2 );

    HOLD OFF ANSWERING JEREMY.

    Strange, I had to do a wp db restore to get site back up, and then the page I wanted excluded (ID=72) was now excluded on site map

    https://apschipsealingpaving.com/sitemap.xml

    Page I wanted excluded (ID=72)
    https://apschipsealingpaving.com/z-example-html-page/

    Very strange, though I had to do the DB restore to yesterday, the code is now in the chnild thme function file, the same code that crashed the site.

    function jeherve_exclude_post_sitemap( $skip, $post ) {
            if ( '72' ==  $post->ID ) {
                    $skip = true;
            } else {
                    $skip = false;
            }
            return $skip;
    }
    add_filter( 'jetpack_sitemap_skip_post', 'jeherve_exclude_post_sitemap', 10, 2 );

    Even Weirder, I delete the code, save the functions file, and page is still gone from sitemap. Getting baffling

    Plugin Author Jeremy Herve

    (@jeherve)

    Jetpack Mechanic ??

    It looks like you had added the code to the child theme at one point, and then added it to the Code Snippets plugin later. Having the snippet twice would indeed generate an error.

    When removing one of the snippets, you would solve the error.

    The page probably still appeared in the sitemap for a few hours after you added the code snippet because we cache the sitemap for a bit to improve performance. When excluding a post with the filter, you would then have to wait until the cache gets cleared to see your changes in the sitemap. To clear the cache, you can publish a new post.

    I hope this clarifies things a bit!

    Thanks Jeremy.

    I’ll try again.

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘Using the jetpack_sitemap_skip_post filter’ is closed to new replies.