I'm trying to get a list of categories (ID, Name). However, only categories that contain products with a certain product attribute value can be listed.

I've already tried the following;

        $categories = Mage::getModel('catalog/category')
        ->addAttributeToFilter('product_supplier', '5');

        foreach($categories as $category) {
            echo $category->getName().'<br>';


        $category = Mage::getModel('catalog/category');
        $catTree = $category->getTreeModel()->load();
        $catIds = $catTree->getCollection()
        ->addAttributeToFilter('product_supplier', '5')
        $cats = array();
        if ($catIds){
            foreach ($catIds as $id){
                $cat = Mage::getModel('catalog/category');
                $cats[$cat->getId()] = $cat->getName();

But, I keep getting the error: Call to a member function getBackend() on a non-object

I found out that this error occurs when the collection tries to filter on the category, so i guess it thinks i i meant to filter by the category attribute product_supplier. I really don't know what to try next.


=> My solution so far:

$catIds = array();
$validCategories = array();

//get the filtered product collection
$productCollection = Mage::getModel('catalog/product')->getCollection()
   ->addAttributeToFilter('product_supplier', '5');

// Loop through products, throw category id's into array
foreach($productCollection as $product) {
    $catIds = array_merge($catIds,$product->getCategoryIds());

 * Loop through category id's, load categories and get name, 
 * create the final array
foreach ($catIds as $x => $catId) {
    $cat = Mage::getModel('catalog/category')->load($catId);
    $validCategories[$cat->getId()] = $cat->getName();
  • Can you give a backtrace in which file this call happens as your code doesn't contain any getBackend() calls Commented Mar 18, 2015 at 11:22
  • It happens in a module i created, in an adminhtml tab. The getBackend, as i mentioned above, gets called when you try to filter a category on a non-existing category-attribute. Alan Storm did a nice article about it here: magento-quickies.alanstorm.com/post/91985359245/… . - That's part of the issue.
    – BBQ.
    Commented Mar 18, 2015 at 11:25

1 Answer 1


I haven't tested this, but here is my idea.
You should start from the product collection because you cannot filter a category collection by a product attribute.

//get the filtered product collection
$productCollection = Mage::getModel('catalog/product')->getCollection()
   ->addAttributeToFilter('product_supplier', '5');

//get a list of all categories
$categories = Mage::getModel('catalog/category')->getCollection()->addAttributeToSelect('*');

//match products to categories
$validCategories = array();
//check which category has a product count higher than 0
foreach ($categories as $category) {
    if ($category->getProductCount()) {
        $validCategories[$category->getId()] = $category->getName();
  • Ah, something like that might work, however, it returns an empty array as for now. It seems the addCountToCategories function doesn't do its job. - I checked if the product i'm testing is active, and was added to active categories, and both are true.
    – BBQ.
    Commented Mar 18, 2015 at 12:23
  • I made a change. I changed addAttributeToSelect('name') into addAttributeToSelect('*'). See if it works like that. I also added for the product collection addAttributeToSelect('*') but not sure if that is needed.
    – Marius
    Commented Mar 18, 2015 at 12:27
  • As soon as addCountToCategories adds to the collection, all products disappear. Obviously, but my "special" product doesn't show up either.
    – BBQ.
    Commented Mar 18, 2015 at 12:41
  • I (temporarily) solved it with my edit in the original post. It's a pretty dirty way tho, especially when the product collection is big.. I really need something cleaner.
    – BBQ.
    Commented Mar 18, 2015 at 13:38
  • I edited my comment, it's far from a final solution.
    – BBQ.
    Commented Mar 18, 2015 at 13:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.