I am working on a collection query of categories:

$categories_check = Mage::getModel('catalog/category')
    ->addFieldToFilter('is_active', 1)       
    ->addAttributeToFilter('path', array('like' => "1/{$rootCategoryId}/%"))   

It returns the categories below root category $rootCategoryId. My requirement is to change this query, that it works for an array of root categories.

Like when I pass the array $rootcat = array('15,20,25,35'); it provides me categories below these root categories.

2 Answers 2



As basic of on your last question Programatically get category collection by multiple root category id .I have give you the answer.

Have create multiple OR condition for addFieldToFilter() for getting the result on basic of path field

You should this script which may be give u your required result.

require_once "YOurMagentoDirapp/Mage.php";
ini_set('display_errors', 1);


$rootId = Mage_Catalog_Model_Category::TREE_ROOT_ID;
$rootids= array(12,15,18,20);

foreach($rootid as $id){

  $buildArray[]= array('attribute' =>'path','like'=>$rootId.'/'.$id.'/%');


$collection = Mage::getModel('catalog/category')->getCollection()

For getting the query of a collection.You can try this.


For you case. use below

echo $categories_check->getSelect()->__toString();

Donot use

  • ->setStoreId($storeId)
  • ->setProductStoreId($storeId)

  • ->setLoadProductCount(1)

  • I want this query also work for array of root category and give it sub category.
    – Mathew
    Commented Oct 7, 2015 at 7:00
  • do you have try my yesterday answer magento.stackexchange.com/a/85448/4564
    – Amit Bera
    Commented Oct 7, 2015 at 7:01
  • i have check his/her last question on basic of that o have asnwer this question
    – Amit Bera
    Commented Oct 7, 2015 at 7:13
  • Why not use above condition in collection "->setStoreId($storeId)" etc.
    – Mathew
    Commented Oct 7, 2015 at 8:15
  • if i use then it may be one store wise collection.. not sure 100%. Was not tried yet'
    – Amit Bera
    Commented Oct 7, 2015 at 8:17

You can pass an array of conditions to addAttributeToFilter, then it will be combined with OR.

The syntax is as follows:

    array('attribute' => 'path', 'like' => "1/15/%"),
    array('attribute' => 'path', 'like' => "1/20/%"),
    array('attribute' => 'path', 'like' => "1/25/%"),
    array('attribute' => 'path', 'like' => "1/35/%"),

Create this array parameter with a loop over your input array and you are set.

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.