Magento 1.9 – Issues with addAttributeToFilter in Category Collection

categoryce-1.9.0.1collection;

I added a new attribute "featured_category" as a boolean.

The issue I'm having is when filtering on a category collection – it seemingly ignores this value in my filtering:

/** @var $categories Mage_Catalog_Model_Resource_Category_Collection */
$categories = Mage::getResourceModel("catalog/category_collection");
$categories->addAttributeToSelect(array("name", "image"))
           ->addAttributeToFilter('is_active', array('eq'=>'1'))
           ->addAttributeToFilter("featured_category", array("eq", '1'));

I originally thought it was a string/integer/boolean type thing, but '1', 1 and "1" all fail to filter the collection.

I'm sure this is going to be something obvious…

— including mysql4-install-0.2.0.php in case it has some impact…

$installer = $this;
$installer->startSetup();

$setup = new Mage_Eav_Model_Entity_Setup('core_setup');

$entityTypeId     = $setup->getEntityTypeId('catalog_product');
$attributeSetId   = $setup->getDefaultAttributeSetId($entityTypeId);
$attributeGroupId = $setup->getDefaultAttributeGroupId($entityTypeId, $attributeSetId);

$setup->addAttribute('catalog_category', 'featured_category', array(
    'type' => 'int',
    'group'     => 'General Information',
    'backend' => '',
    'frontend' => '',
    'label' => 'Featured Category (Home Page)',
    'input' => 'select',
    'class' => '',
    'source' => 'eav/entity_attribute_source_boolean',
    'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
    'visible' => true,
    'required' => false,
    'user_defined' => false,
    'default' => '0',
    'searchable' => false,
    'filterable' => false,
    'comparable' => false,
    'visible_on_front' => false,
    'unique' => false,
));

$setup->addAttributeToGroup(
    $entityTypeId,
    $attributeSetId,
    $attributeGroupId,
    'featured_category',
    '13'//last Magento's attribute position in General tab is 10
);

$installer->endSetup();

Best Answer

Not 100% sure, but I think this is the problem.

->addAttributeToFilter("featured_category", array("eq", '1'));

Should be

->addAttributeToFilter("featured_category", array("eq"=>'1'));

or just simply

->addAttributeToFilter("featured_category", 1);