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.