Skip to contentSkip to main navigation Skip to footer

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

Squirrly SEO Exclude Out-of-Stock Products from the Sitemap with a Squirrly Hook screen on a laptop

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.

PropertyWhat Squirrly does with it
$post->sq->noindextrue adds noindex to the robots meta of the page
$post->sq->nofollowtrue adds nofollow to the robots meta of the page
$post->sq->do_sitemapfalse 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

  1. Make a backup of the file you’ll edit, or use a child theme so a theme update doesn’t remove the code.
  2. Open the functions.php file of your child theme, or a small custom plugin.
  3. Paste the code below at the end of the file.
  4. 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

  1. Open one product in WooCommerce, go to the Inventory tab and set Stock status to Out of stock. Update the product.
  2. Go to Squirrly > Technical SEO > Tweaks & Sitemaps > Sitemap XML and click Clear Sitemap Cache.
  3. Open https://yourdomain.com/sitemap-product.xml. The product is gone from the list.
  4. Go to Products > All Products. The Squirrly column of that product shows NoIndex and NoFollow.
  5. 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.

Was This Article Helpful?

0 Comments

There are no comments yet

Leave a comment

Before you leave

Future of AEO GEO and AI Search!

Free Now. Free Forever.

It’s time to stop orbiting SaaS.

Be the Meteor.

10 Elite Tools. 1 Stack.