Exclude Out-of-Stock Products from the Sitemap with a Squirrly Hook

A short PHP filter on the Squirrly SEO hook sq_post removes out-of-stock WooCommerce products from the Squirrly sitemap and adds noindex and nofollow to their pages. Squirrly SEO has no screen option for stock status, so this is done in code.
The filter was checked against the Squirrly SEO 14.2.9 code.
How the filter works
Squirrly passes every post it loads through the sq_post filter: the page a visitor opens, each URL it puts in the sitemap, and each row of the Squirrly column in the WordPress lists. The post object carries an sq object with the SEO settings of that page.
| Property | What Squirrly does with it |
|---|---|
| $post->sq->noindex | true adds noindex to the robots meta of the page |
| $post->sq->nofollow | true adds nofollow to the robots meta of the page |
| $post->sq->do_sitemap | false leaves the URL out of every Squirrly sitemap |
The filter below runs with priority PHP_INT_MAX, after Squirrly has applied your Automation rules and the product’s own SEO Snippet. It changes only products where WooCommerce reports is_in_stock() as false.
Add the filter
- Make a backup of the file you’ll edit, or use a child theme so a theme update doesn’t remove the code.
- Open the functions.php file of your child theme, or a small custom plugin.
- Paste the code below at the end of the file.
- Save the file and open your site to check that it still loads.
add_filter('sq_post', function ($post){
//If the current post is a product
if ($post->post_type == 'product' && function_exists('wc_get_product')) {
//get the product from woocommerce
$product = wc_get_product( $post->ID );
//If the product exists and has the property is_in_stock
if ($product && method_exists($product, 'is_in_stock')) {
//if the product is out of stock
if(!$product->is_in_stock()){
//exclude the product from Squirrly sitemap
$post->sq->noindex = true;
$post->sq->nofollow = true;
$post->sq->do_sitemap = false;
}
}
}
return $post;
}, PHP_INT_MAX, 1);The function must return $post. Without the return line, Squirrly gets an empty post back and can’t output the SEO data of the page.
To keep out-of-stock products indexed and only drop them from the sitemap, delete the two lines with noindex and nofollow.
Check the result
- Open one product in WooCommerce, go to the Inventory tab and set Stock status to Out of stock. Update the product.
- Go to Squirrly > Technical SEO > Tweaks & Sitemaps > Sitemap XML and click Clear Sitemap Cache.
- Open https://yourdomain.com/sitemap-product.xml. The product is gone from the list.
- Go to Products > All Products. The Squirrly column of that product shows NoIndex and NoFollow.
- Open the product page, view the source and look for noindex in the robots meta tag.
The Squirrly column reads the same filter, which is why the badges change in the products list. When the product is back in stock, it returns to the sitemap after you clear the sitemap cache.
LLM Indexing skips noindex posts in its automatic submits, so an out-of-stock product with this filter isn’t sent to IndexNow when you save it.
Troubleshooting
The product is still in the sitemap
The sitemap cache still holds the old copy. Click Clear Sitemap Cache on the Sitemap XML tab, then purge your page cache for /sitemap-product.xml.
The site shows a critical error after saving functions.php
The paste broke the PHP syntax. Restore the backup by FTP or the hosting file manager, then paste the code again at the end of the file, outside any other function.
Nothing changes at all
Check that the code sits in the theme that is active, or in a plugin that is active. The filter only runs where WordPress loads that file.
Frequently asked questions
Do I need the Advanced Pack for this filter?
No, the sq_post filter is in the free Squirrly SEO plugin.
Does the filter remove the product from the shop?
No, it only changes what Squirrly outputs for search engines.
Can I exclude other products the same way?
Yes, change the condition, for example to a product category, and keep the three sq lines.
Related tutorials
- Sitemap XML Settings: the Products sitemap and the cache.
- Deactivating Specific Features in Squirrly SEO Plugin Using Hooks: other filters for developers.
- Hook Squirrly Title & Description With Custom Data: another sq filter example.
- Add Custom Noindex Meta: noindex for one page from the SEO Snippet.
- Does Squirrly work with WooCommerce?: the WooCommerce features in Squirrly.




0 Comments