Automatic sitemap exclusion based on product stock
-
I was toying around with Woocommerce recently. Most webshops I work on have a feature enabled to automatically hide a product from the site (both internal navigation as well as search functions) once the product is sold out.
Handy and clean, but creates a lot of orphan pages.Then I found this script to automatically add a noindex tag to products which seemed to solve this problem, however now we’ve got a lot of noindex-pages in the sitemap.
So I found the Yoast developer api documentation and since I am not too familiar with .php then I’d like to ask for some help in making a function to exclude sold-out products from the sitemap.
https://developer.yoast.com/features/xml-sitemaps/api/#exclude-specific-posts
function add_tagseo_metarob() { if ( get_post_type( get_the_ID() ) == 'product'){ $pro = new WC_Product(get_the_ID()); if( $pro->stock_status != 'instock' ){ ?> <meta name="robots" content="noindex"> <?php } } } add_action('wp_head', 'add_tagseo_metarob');
This is the code used to add the noindex tag, relatively simple in that it just checks the stock and if it’s not instock, then adds the noindex tag.
I was thinking of something like this, but I am not too well-versed in .php so I’d like to guideance here
function sitemap_exclude_soldout( $excluded, $post_type ) { return $pro = new WC_Product(get_the_ID()); if( $pro->stock_status != 'instock' ); } add_filter( 'wpseo_sitemap_exclude_post_type', 'sitemap_exclude_soldout', 10, 2 );
- The topic ‘Automatic sitemap exclusion based on product stock’ is closed to new replies.