PHP code to exclude specific category page from sitemap.xml in Magento

PHP code to exclude specific category page from sitemap.xml in Magento

September 30, 2026 · Illustrated author avatarBy Stefan Bogdanescu
PHP code to exclude specific category page from sitemap.xml in Magento

Excluding Specific Category Pages from Magento Sitemap.xml Using PHP

Are you looking to exclude certain category pages from your Magento website’s sitemap.xml file? You’ve come to the right place! In this post, we’ll guide you through the process of modifying your Catalog/Category.php file to achieve exactly that. Let’s dive in!

Understanding the Problem

You want to include only specific pages from your Magento site in the sitemap.xml while excluding certain category pages from being indexed by search engines. You’ve already successfully modified the Cms/Page.php file using the directions provided here. However, you’re unsure how to adapt this solution for the Category.php file.

The Current Category.php File

Here’s your current Category.php file:

<?php

class Mage_Sitemap_Model_Resource_Catalog_Category extends Mage_Core_Model_Resource_Db_Abstract
{
    // ... (rest of the code)

    protected function _construct()
    {
        $this->_init('catalog/category', 'entity_id');
    }

    public function getCollection($storeId)
    {
        // ... (rest of the code)
    }

    protected function _prepareCategory(array $categoryRow)
    {
        // ... (rest of the code)
    }

    protected function _addFilter($storeId, $attributeCode, $value, $type = '=')
    {
        // ... (rest of the code)
    }
}

As you can see, this file is responsible for retrieving category collections based on certain conditions. However, it currently doesn’t have any logic to exclude specific categories.

Solving the Problem

To exclude specific category pages from your sitemap.xml, follow these steps:

  1. Open your Category.php file located at app/code/local/Mage/Sitemap/Model/Resource/Catalog/Category.php.

  2. Add a protected property $ignoreCategories to store the IDs of the categories you want to exclude:

protected $ignoreCategories = array("2","3","16");//Replace the numbers with your category IDs

In this example, we’re excluding categories with IDs 2, 3, and 16. You can replace these numbers with the actual IDs of the categories you want to exclude.

  1. Now, modify the getCollection() method as follows:
public function getCollection($storeId)
{
    // ... (rest of the initial code)

    while ($row = $query->fetch()) {

        // Added to exclude specific categories
        if(in_array($this->_prepareCategory($row)->getId(), $this->ignoreCategories))
        {
            continue;
        }

        $category = $this->_prepareCategory($row);
        $categories[$category->getId()] = $category;
    }

    return $categories;
}

This code snippet checks if the current category’s ID is present in the $ignoreCategories array. If it is, the continue; statement is executed, moving on to the next iteration of the loop and effectively excluding that category from the sitemap.xml.

  1. Save your changes and clear your Magento cache.

Conclusion

In this post, we’ve demonstrated how to exclude specific category pages from your Magento website’s sitemap.xml using PHP. By modifying the Catalog/Category.php file as described above, you can easily control which categories are included or excluded in the sitemap.

Remember that proper SEO practices require careful consideration of what content should be indexed and what shouldn’t. Excluding certain categories can help improve your site’s relevance to search engines and enhance user experience.

If you found this solution helpful, feel free to explore more Magento-related resources on our website at https://laravelcompany.com/. We’re always here to assist you with your e-commerce development needs!

Catalog API SEO Errors

Our network