• Resolved ibrahimpak

    (@ibrahimpak)


    I want to exclude out of stock products in sitemaps.

    I am trying the following code for this, but then the product sitemap link does not open and it says “a critical error has been encountered” can anyone help?

    Here is the code

    add_filter( 'wp_sitemaps_posts_entry', 'exclude_no_stock_products',10,2);
     function exclude_no_stock_products( $entry, $post ) {
       if ( ! $product->is_in_stock() ) { 
           return false;
       }
       return $entry;
    }
Viewing 4 replies - 1 through 4 (of 4 total)
  • Mirko P.

    (@rainfallnixfig)

    Hi @ibrahimpak,

    Support for custom code is limited on this forum, but I’m going to leave this thread open for a bit to see if anyone is able to chime in to help you out.

    I can also recommend the WooCommerce Developer Resources Portal for resources on developing for WooCommerce.

    The easiest solution would be to use an SEO plugin and set all the URLs you want to exclude from the sitemap as noindex.

    For help with custom code you can also visit the WooCommerce Facebook Community group or the #developers channel of the WooCommerce Community Slack. We’re lucky to have a great community of open-source developers for WooCommerce, and many of our developers hang out there, as well.

    Cheers.

    Thread Starter ibrahimpak

    (@ibrahimpak)

    Hello @rainfallnixfig

    Does adding noindex to links work even if they are in the sitemap?

    If so, there will be no need to edit sitemaps.

    Mirko P.

    (@rainfallnixfig)

    Hi @ibrahimpak,

    If URLs are already in the sitemap web crawlers might read them as they’ll read the XML or HTML file, but the content of those URLs will not be indexed if they’re set to noindex.

    There are two ways to exclude posts or pages from the sitemap: one is using an SEO plugin as mentioned above and the other is adding the post ids to be removed in the functions.php file using custom code (but this is a bit different from removing out of stock products as the ids need to be entered manually). You can find more details on these tutorials:

    * https://preventdirectaccess.com/5-ways-remove-pages-from-sitemap/#how
    * https://www.carnaghan.com/exclude-pages-or-posts-from-sitemap-wordpress/

    Regarding your specific code, we’re going to leave the thread open to see if anyone can chime in and figure out why is generating that critical error.

    Cheers.

    Thread Starter ibrahimpak

    (@ibrahimpak)

    Now I understand better how the noindex tag actually works.

    This will solve the index exclusion issue for out of stock items. Because even if search engine bots see this link, they will not index it.

    Thanks for your recommendation @rainfallnixfig

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Exclude out of stock products from sitemap’ is closed to new replies.