Support » Plugin: XML Sitemap & Google News » `filter´ to exclude posts from being included in sitemap.xml?

  • Resolved gerd.neumann

    (@gerdneumann)


    Hi,

    happy I have found your plugin.

    At the moment I am using https://de.wordpress.org/plugins/google-sitemap-generator/ but I am not too happy with it: almost no support, no development, seems not very active. Images in posts are not included and so on… …and no filters (see below).

    some questions:

    1. I am using Groups to have some internal products (woocommerce). So when not logged in (google bot) you get a 404. The problem is that these products would still be part in the sitemap.xml. I saw the option “Exclude from XML Sitemap” but it would be easier (and more error-prove) if there was a filter to which could be used to filter (reduce) all post IDs which are “blocked” by Groups plugin.

    Is there such a filter?

    2. Do you support WooCommerce products? Are these included in the sitemap.xml?

    3. Anything else to think of when migrating from https://de.wordpress.org/plugins/google-sitemap-generator/ (Checking Google search console for sure, but is there any missing feature I cannot think of at the moment?)

    Kind regards,
    Gerd

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author RavanH

    (@ravanh)

    Hi,

    The plugin supports any custom post type, Products too, and it is designed to exclude individual posts with the Private status. But I cannot say in advance this will successfully filter out these “Internal Products” in your case as I’m not sure what status these actually have.

    But if it does not filter out these non-public products, then yes, you might be able use the filter xmlsf_excluded which filters the response of the exclusion option for each post/page/product. For example:

    
    function is_internal_product( $id ) {
        // do your check for internal product here and return either false or true
    }
    
    function filter_internal_products( $response, $id ) {
        if ( 'product' == get_post_type() && is_internal_product( $id ) ) {
            $response = true; // force the exclude flag to true
        }
    
        return $response;
    }
    add_filter( 'xmlsf_excluded', 10, 2 );
    

    Now it depends a bit on how you intend to verify if a product is such an internal product but since this is happening within a normal WordPress loop, you can also include a global $post with which you can quickly access the most important post data.

    Hope you can make it work for you. Let me know if you need more help 🙂

    To answer your last question: no I cannot think of anything when migrating, except maybe you need to make sure the other sitemap plugin does not leave a static sitemap.xml file in your site root…

    Wow, great answer! I’ll try this. So happy I found your plugin 🙂

    Plugin Author RavanH

    (@ravanh)

    Glad to help. Let me know if you run into any issues 🙂

    I just noticed an error in the last line of the code example. It should read:

    
    add_filter( 'xmlsf_excluded', 'filter_internal_products', 10, 2 );
    

    I’ve set it up like this, but my code is never called. I tried to set a breakpoint and also log some message. The add_filter is executed but still the filter_internal_products is never stepped in.

    Code is:

    in functions.php of child theme:

    
    require_once('includes/sitemap.php');
    

    and in the includes/sitemap.php:

    
    function filter_internal_products( $response, $id ) {
      // THIS IS NEVER CALLED
      error_log('in filter function');
      if ( is_product() && fr_is_internal_product( $id ) ) {
          $response = true; // force the exclude flag to true
      }
      return $response;
    }
    
    error_log('outside filter function');
    add_filter( 'xmlsf_excluded', 'filter_internal_products', 10, 2 );
    

    error.log says:

    
    [21-Jun-2018 06:17:02 UTC] outside filter function
    [21-Jun-2018 06:17:04 UTC] outside filter function
    

    So it is executed twice (which is also strange), but only outside.

    Is there some kind of caching by the plugin so that xmlsf_excluded is not always called or similar?

    Uh, forget what I wrote. It’s just not working for sitemap.xml but works for sitemap-posttype-page.xml

    Sory, about it.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘`filter´ to exclude posts from being included in sitemap.xml?’ is closed to new replies.