• Resolved drimproove

    (@drimproove)


    Hi,

    I would like to know is does exist the possibility of bulk exclude from the sitemap.xml certain posts.

    I mean, I have about 2000 posts I nedd to exclude from sitemap.xml. Do this thing one by one is impossible to me. Is there any possibility to add the posts IDs and exclude them from sitemap?

    Best regards

Viewing 4 replies - 1 through 4 (of 4 total)
  • Hi, no not possible yet. I’ve planned adding the exclude option to the bulk editor on the Posts list but can’t say when that will arrive…

    What you could do in the mean time is use the existing xmlsf_excluded filter. Create a new new file wp-content/mu-plugins/sitemap-exclude.php in a plain text editor (like notepad or anything that does not add markup code!)

    
    <?php
    
    // filter exclude posts
    function drimproove_exclude_posts( $excluded, $post_id ) {
      // add your excluded post id's here
      $exclude_array = array( 1, 2, 3, 4 ); 
    
      if ( in_array( $post_id, $exclude_array ) ) $excluded = true;
    
      return $excluded;
    }
    
    add_filter('xmlsf_excluded','drimproove_exclude_posts',10,2);
    
    

    Code is not tested so please prepare to delete the file again if your site goes blank 🙂

    Thread Starter drimproove

    (@drimproove)

    It worked perfectly! Thank you very much for your support and your quick answer!

    Regards

    Thread Starter drimproove

    (@drimproove)

    Hi again,

    So I tested that code with two post’s IDs and it worked perfectly but when I tested the code with 2000 IDs (is the amount of posts that I need to exclude), some errors appeared.

    Error #1

    Fatal error: Cannot redeclare drimproove_exclude_posts() (previously declared in /xxx/mu-plugins/sitemap-exclude.php:3) in /xxx/mu-plugins/sitemap-exclude_test.php on line 10

    I solved this error deleting the test file I’ve created on the same directory

    Error #2

    Warning: Cannot modify header information – headers already sent by (output started at /xxx/mu-plugins/sitemap-exclude.php:17) in /xxx/plugins/sg-cachepress/core/Supercacher/Supercacher_Helper.php on line 68

    So I deleted the plugin and no error messages are shown, but…now the sitemaps are shown with no format, like plain text.

    I have made some test on Search Console, and it seems to be ok, so I’ll be monitoring it.

    Thanks for sour support

    Hi, the first error is very logic if you create two plugin files with the same function (name), then this error will occur. Keep just one file 🙂

    The second error will occur when there is something echoed or otherwise parsed as HTML in the PHP file. This will cause the server to start sending HTML with a header too soon, hence the “headers already sent” error later.

    HTML output can be something that comes after echo "..."; or even a simple blank line in a PHP file that comes before the opening <?php or after the closing ?> (does not need to be there) PHP tags.

    Check the file you created and:
    1. make sure nothing is echoed there.
    2. if you a closing ?> anywhere: if at the end of the file then simply remove it, if somewhere inside the function then paste your code in a reply…
    3. make sure there is no blank line at the start of the file before the <?php tag

    If the headers already sent error still occurs, please paste the code you have now in a reply (use the CODE button above the text field to start and and the code block with a back-tick sign) so I can see what else is wrong 🙂

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

The topic ‘Exclude certain posts in bulk’ is closed to new replies.