I have this code for getting a list of category IDs:

$categoryModel = Mage::getModel('catalog/category')->load($id);
$allCategoryIds = $category->getResource()->getChildren($category, true);

This gives me a nice little array with a list of category IDs, like this:

Array([0] => 10, [1] => 11, [2] => 3, [3] => 4, [4] => 5, [5] => 6, [6] => 7, [7] => 14, [8] => 17, [9] => 18);

What I want to do is order this array by the position these categories appear in the Admin Panel.

To begin with I looked into these solutions:

However both of these solutions end up using an entire collection, and then sorting that collection. To get my array sorted as I would like, I would have to iterate through the entire collection and use that to re-order my array. This seems wrong / wasteful to me (please correct me if I am wrong here).

I am wondering if an SQL call be made to the database to get a simple array of Category IDs and their position in the Admin Panel? Would this be quicker and more efficient than using Magento's system? I would rather use Magento's system if possible, but not if it involves iterating over several large objects (this page is already doing a lot of work).

So my question is:

What is the easiest and most efficient way of getting just an array of category IDs (like above), but in order of of they are displayed in the Admin Panel?

1 Answer 1


If you take a look at the Mage_Catalog_Model_Resource_Category class method getChildren you can see what it does.

Building your collection like

$collection = Mage::getModel('catalog/category')->getCollection()
  ->addAttributeToFilter('path', array('like' => "{$categoryModel->getPath()}/%}"));

$allCategoryIds = $collection->getAllIds();

Will do basically the same limiting the query to only the catalog_category_entity table.

  • you are fully pick form .:)
    – Amit Bera
    Nov 10, 2015 at 14:12
  • Hi, thanks for this! Unfortunately it is returning an empty array?!?
    – Jimmery
    Nov 10, 2015 at 15:11
  • @Jimmery maybe try without the store filter and make sure that the path is correct. Nov 10, 2015 at 15:15
  • took out the store filter, the path is 1/2, 2 is the ID of the root category. still an empty array :(
    – Jimmery
    Nov 10, 2015 at 15:28

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.