title: PHP code to exclude specific category page from sitemap.xml in Magento
body: <p>I want to include those pages from my Magento website's <code>sitemap.xml</code> which should not be indexed by search engines. For this I have copied <code>Catalog/Category.php Cms/Page.php</code> to my corresponding <code>app/code/local</code> directories and I'm modifying it.</p>
  
  <p>I have modified the <code>Page.php</code> successfully to exclude specific CMS pages from my <code>sitemap.xml</code>. I followed the directions given <a href="https://stackoverflow.com/questions/12528883/exclude-specific-page-from-sitemap-xml-generation">here</a></p>
  
  <p>As I'm not familiar with PHP, I don't know what changes to make to adapt this solution for the <code>Category.php</code> file.</p>
  
  <p>Here's my present <code>Category.php</code> file which is not excluding any specific category pages I want from <code>sitemap.xml</code>:</p>
  
  <pre><code> &lt;?php
  
  class Mage_Sitemap_Model_Resource_Catalog_Category extends Mage_Core_Model_Resource_Db_Abstract
  {
  /**
   * Collection Zend Db select
   *
   * @var Zend_Db_Select
   */
  protected $_select;
  
  /**
   * Attribute cache
   *
   * @var array
   */
  protected $_attributesCache    = array();
  
  /**
   * Init resource model (catalog/category)
   *
   */
  protected function _construct()
  {
      $this-&gt;_init('catalog/category', 'entity_id');
  }
  
  /**
   * Get category collection array
   *
   * @param unknown_type $storeId
   * @return array
   */
  public function getCollection($storeId)
  {
      $categories = array();
  
      $store = Mage::app()-&gt;getStore($storeId);
      /* @var $store Mage_Core_Model_Store */
  
      if (!$store) {
          return false;
      }
  
      $this-&gt;_select = $this-&gt;_getWriteAdapter()-&gt;select()
          -&gt;from($this-&gt;getMainTable())
          -&gt;where($this-&gt;getIdFieldName() . '=?', $store-&gt;getRootCategoryId());
      $categoryRow = $this-&gt;_getWriteAdapter()-&gt;fetchRow($this-&gt;_select);
  
      if (!$categoryRow) {
          return false;
      }
  
      $urConditions = array(
          'e.entity_id=ur.category_id',
          $this-&gt;_getWriteAdapter()-&gt;quoteInto('ur.store_id=?', $store-&gt;getId()),
          'ur.product_id IS NULL',
          $this-&gt;_getWriteAdapter()-&gt;quoteInto('ur.is_system=?', 1),
      );
      $this-&gt;_select = $this-&gt;_getWriteAdapter()-&gt;select()
          -&gt;from(array('e' =&gt; $this-&gt;getMainTable()), array($this-&gt;getIdFieldName()))
          -&gt;joinLeft(
              array('ur' =&gt; $this-&gt;getTable('core/url_rewrite')),
              join(' AND ', $urConditions),
              array('url'=&gt;'request_path')
          )
  
          -&gt;where('e.path LIKE ?', $categoryRow['path'] . '/%');
  
      $this-&gt;_addFilter($storeId, 'is_active', 1);
  
      $query = $this-&gt;_getWriteAdapter()-&gt;query($this-&gt;_select);
      while ($row = $query-&gt;fetch()) {
          $category = $this-&gt;_prepareCategory($row);
          $categories[$category-&gt;getId()] = $category;
      }
  
      return $categories;
  }
  
  /**
   * Prepare category
   *
   * @param array $categoryRow
   * @return Varien_Object
   */
  protected function _prepareCategory(array $categoryRow)
  {
      $category = new Varien_Object();
      $category-&gt;setId($categoryRow[$this-&gt;getIdFieldName()]);
      $categoryUrl = !empty($categoryRow['url']) ? $categoryRow['url'] : 'catalog/category/view/id/' . $category-&gt;getId();
      $category-&gt;setUrl($categoryUrl);
      return $category;
  }
  
  /**
   * Add attribute to filter
   *
   * @param int $storeId
   * @param string $attributeCode
   * @param mixed $value
   * @param string $type
   * @return Zend_Db_Select
   */
  protected function _addFilter($storeId, $attributeCode, $value, $type = '=')
  {
      if (!isset($this-&gt;_attributesCache[$attributeCode])) {
          $attribute = Mage::getSingleton('catalog/category')-&gt;getResource()-&gt;getAttribute($attributeCode);
  
          $this-&gt;_attributesCache[$attributeCode] = array(
              'entity_type_id'    =&gt; $attribute-&gt;getEntityTypeId(),
              'attribute_id'      =&gt; $attribute-&gt;getId(),
              'table'             =&gt; $attribute-&gt;getBackend()-&gt;getTable(),
              'is_global'         =&gt; $attribute-&gt;getIsGlobal(),
              'backend_type'      =&gt; $attribute-&gt;getBackendType()
          );
      }
  
      $attribute = $this-&gt;_attributesCache[$attributeCode];
  
      if (!$this-&gt;_select instanceof Zend_Db_Select) {
          return false;
      }
  
      switch ($type) {
          case '=':
              $conditionRule = '=?';
              break;
          case 'in':
              $conditionRule = ' IN(?)';
              break;
          default:
              return false;
              break;
      }
  
      if ($attribute['backend_type'] == 'static') {
          $this-&gt;_select-&gt;where('e.' . $attributeCode . $conditionRule, $value);
      } else {
          $this-&gt;_select-&gt;join(
              array('t1_'.$attributeCode =&gt; $attribute['table']),
              'e.entity_id=t1_'.$attributeCode.'.entity_id AND t1_'.$attributeCode.'.store_id=0',
              array()
          )
          -&gt;where('t1_'.$attributeCode.'.attribute_id=?', $attribute['attribute_id']);
  
          if ($attribute['is_global']) {
              $this-&gt;_select-&gt;where('t1_'.$attributeCode.'.value'.$conditionRule, $value);
          } else {
              $ifCase = $this-&gt;_select-&gt;getAdapter()-&gt;getCheckSql('t2_'.$attributeCode.'.value_id &gt; 0', 't2_'.$attributeCode.'.value', 't1_'.$attributeCode.'.value');
              $this-&gt;_select-&gt;joinLeft(
                  array('t2_'.$attributeCode =&gt; $attribute['table']),
                  $this-&gt;_getWriteAdapter()-&gt;quoteInto('t1_'.$attributeCode.'.entity_id = t2_'.$attributeCode.'.entity_id AND t1_'.$attributeCode.'.attribute_id = t2_'.$attributeCode.'.attribute_id AND t2_'.$attributeCode.'.store_id=?', $storeId),
                  array()
              )
              -&gt;where('('.$ifCase.')'.$conditionRule, $value);
          }
      }
  
      return $this-&gt;_select;
  } 
  }
  </code></pre>
  
  <p><b>Edit</b>: Many thanks to Luke in his comment below! Here is what worked for me. I had to modify the code a bit since $category isn't defined until after the if statement.</p>
  
  <pre><code>while ($row = $query-&gt;fetch()) {
  
  // Added to exclude specific categories
  if(in_array($this-&gt;_prepareCategory($row)-&gt;getId(), $this-&gt;ignoreCategories))
  {
      continue;
  }
  
  $category = $this-&gt;_prepareCategory($row);
  $categories[$category-&gt;getId()] = $category;
  }
  </code></pre>
  
  <p>I created the class property for the category IDs just as Luke had said:</p>
  
  <pre><code>protected $ignoreCategories = array("2","3","16");//Replace the numbers with your category IDs
  </code></pre>