• Hi,

    I’m trying to follow:

    specifically the excluding a single post from the sitemap part. This was the code that was suggested:

    add_filter(
        'wp_sitemaps_posts_query_args',
        function( $args, $post_type ) {
            if ( 'post' !== $post_type ) {
                return $args;
            }
     
            $args['post__not_in'] = isset( $args['post__not_in'] ) ? $args['post__not_in'] : array();
            $args['post__not_in'][] = 123; // 123 is the ID of the post to exclude.
            return $args;
        },
        10,
        2
    );

    I replaced 123 with the correct post ID but it’s still displaying the page on the sitemap. Do I need to add anything if it’s for a page since this instruction is for a post.

    Please help. Thanks

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator threadi

    (@threadi)

    It works for me without any problems. I’m confused though: are you talking about a post or a page?

    Furthermore: where did you insert the code? Could you check by test outputs whether it is really executed?

    Thread Starter ninjja

    (@ninjja)

    @threadi , thanks for checking. I want to hide a page from my sitemap. Specifically a thank you page. So I went to that post and grabbed the post id and added it in that code.

    I added the code to the functions.php. How do I test whether it was executed? To add, I hid the users from the sitemap and the code was working/being executed, it’s only that hiding of page from the sitemap that’s not working.

    Moderator threadi

    (@threadi)

    Sounds like the right thing you did. However, you’re talking about a page, not a post. Change this line

    if ( 'post' !== $post_type ) {

    to

    if ( 'page' !== $post_type ) {

    then it should work.

    • This reply was modified 3 years, 5 months ago by threadi.
    Thread Starter ninjja

    (@ninjja)

    @threadi , thanks for that info. I ended up going with SlimSEO late last night as I just found out that having a last modified date in the sitemap which are included by default in almost every seo plugins (at least the ones I tried) is very important for search engines.

    I will give the code you gave a shot in future builds as I’ve already submitted my sitemap URL to GSC. Thanks again for diagnosing this with me!

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

The topic ‘Editing wp-sitemap.xml’ is closed to new replies.