title: Filter a product collection by two categories in Magento
body: <p>I'm trying to find products that are in two categories.
  I've found an example to get products that are in category1 OR category2.
  <a href="http://www.alphadigital.cl/blog/lang/en-us/magento-filter-by-multiple-categories.html" rel="nofollow noreferrer">http://www.alphadigital.cl/blog/lang/en-us/magento-filter-by-multiple-categories.html</a>
  I need products that are in category1 AND category2.</p>
  
  <p>The example in the blog is:</p>
  
  <pre><code>class ModuleName_Catalog_Model_Resource_Eav_Mysql4_Product_Collection
    extends Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection{
  
    public function addCategoriesFilter($categories){
  
    $alias = 'cat_index';
    $categoryCondition = $this-&gt;getConnection()-&gt;quoteInto(
      $alias.'.product_id=e.entity_id AND '.$alias.'.store_id=? AND ',
      $this-&gt;getStoreId()
    );
  
    $categoryCondition.= $alias.'.category_id IN ('.$categories.')';
  
    $this-&gt;getSelect()-&gt;joinInner(
      array($alias =&gt; $this-&gt;getTable('catalog/category_product_index')),
      $categoryCondition,
      array('position'=&gt;'position')
    );
  
    $this-&gt;_categoryIndexJoined = true;
    $this-&gt;_joinFields['position'] = array('table'=&gt;$alias, 'field'=&gt;'position' );
  
    return $this;
  
    }
  }
  </code></pre>
  
  <p>When I'm using this filter alone it perform OR query on several categories.
  When I combine this filter with prepareProductCollection of Mage_Catalog_Model_Layer
  it somehow remove the filter effect.</p>
  
  <p>How can I change the filter to AND and combine it with prepareProductCollection?</p>
  
  <p>Thanks</p>
  
  <p>Thanks</p>