How To

How to Remove Some CMS Pages from Sitmap.xml 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!

Click to rate this post!
[Total: 4 Average: 5]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate 🎖️ Certified Magento Developer👨‍💻. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.🏏

Recent Posts

How to Integrate ChatGPT with Laravel Application?

In this guide, we'll explore how to integrate ChatGPT, an AI-powered chatbot, with a Laravel…

2 days ago

What are Net Sales? How to Calculate Your Net Sales?

In the world of business, understanding financial metrics is crucial for making informed decisions and…

4 days ago

Magento 2 Extensions Digest April 2024 (New Release & Updates)

Welcome to the MageComp Monthly Digest, where we bring you the latest updates, releases, and…

4 days ago

The ABCs of Geofencing: Definition, Features and Uses

In this era, businesses are always on the lookout for ways to engage with their…

5 days ago

How to Delete Product Variant in a Shopify Remix App using GraphQL Mutations?

Managing a Shopify store efficiently involves keeping your product catalog organized. This includes removing outdated…

6 days ago

6 Innovative Tools Revolutionizing E-Commerce Operations

E-commerce has transformed the way consumers shop for products and services and interact with businesses.…

1 week ago