Site icon MageComp Blog

How to Remove Some CMS Pages from Sitmap.xml in Magento 2

How to Remove Some CMS Pages from Sitmapxml in Magento 2

Hello Magento Friends,

Today’s topic is on How to Remove Some CMS Pages from Sitmap.xml in Magento 2.

Before starting, check out Magento 2 Sitemap (Everything You Need to Know)

Sitemaps are used to help search engines crawl your website pages that are usually overlooked. When you enable sitemap for your website, Magento creates a file named sitemap.xml. There may be a necessity to remove some CMS pages from the sitemap.xml file in Magento 2.

Let’s learn How to Remove Some CMS Pages from Sitmap.xml in Magento 2.

Steps to Remove Some CMS Pages from Sitmap.xml in Magento 2:

Step 1: First of all, create a di.xml file in your module directory

app\code\Vendor\Extension\etc\di.xml

Now add the below code.

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
  <preference for="Magento\Sitemap\Model\Sitemap" type="Vendor\Extension\Model\Sitemap"/>
</config>

Step 2: Create a Sitemap.php file at your module directory

app\code\Vendor\Extension\Model\Sitemap.php

Now add the code as given below

<?php

namespace Vendor\Extension\Model;

class Sitemap
{
public function collectSitemapItems()
    {
        $helper = $this->_sitemapData;
        $storeId = $this->getStoreId();
        $this->_storeManager->setCurrentStore($storeId);
        $this->addSitemapItem(new DataObject(
            [
                'changefreq' => $helper->getCategoryChangefreq($storeId),
                'priority' => $helper->getCategoryPriority($storeId),
                'collection' => $this->_categoryFactory->create()->getCollection($storeId),
            ]
        ));

        $this->addSitemapItem(new DataObject(
            [
                'changefreq' => $helper->getProductChangefreq($storeId),
                'priority' => $helper->getProductPriority($storeId),
                'collection' => $this->_productFactory->create()->getCollection($storeId),
            ]
        ));

        $this->addSitemapItem(new DataObject(
            [
                'changefreq' => $helper->getPageChangefreq($storeId),
                'priority' => $helper->getPagePriority($storeId),
                'collection' => $this->_cmsFactory->create()->getCollection($storeId),
            ]
        ));
    }
}

Conclusion:

Hence, this way you can Remove Some CMS Pages from Sitmap.xml in Magento 2. If you face any difficulties, mention them in the comment section, I will be quick to solve them. Share the article further and stay updated with us.

Happy Coding!

Exit mobile version