Change sort order of Magento subcategories

Change sort order of Magento subcategories

July 26, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Change sort order of Magento subcategories

Sorting Magento Subcategories by Admin Order

When working with subcategories in Magento, you might find that the default sorting is based on category ID. However, sometimes you want to display your subcategories in the order specified by the admin user through drag-and-drop functionality.

The current code provided loads all subcategories of a given parent category and displays them sorted by their IDs:

<code>
             &lt;?php
                $currentCat = Mage::registry('current_category');

                if ( $currentCat->getParentId() == Mage::app()->getStore()->getRootCategoryId() )
                {
                    // current category is a toplevel category
                    $loadCategory = $currentCat;
                }
                else
                {
                    // current category is a sub-(or subsub-, etc...)category of a toplevel category
                    // load the parent category of the current category
                    $loadCategory = Mage::getModel('catalog/category')->load($currentCat->getParentId());
                }
                $subCategories = explode(',', $loadCategory->getChildren());

                foreach ( $subCategories as $subCategoryId )
                {
                    $cat = Mage::getModel('catalog/category')->load($subCategoryId);

                    if($cat->getIsActive())
                    {
                        echo '&lt;a href="'.$cat->getURL().'"&gt;'.$cat->getName().'&lt;/a&gt;';
                    }
                }
            ?&gt;
</code>

Loading Categories by Sort Order

To achieve the desired sorting, we need to use a different method when loading the categories. Magento stores the sort order for each category in the sort_order field of its database table.

Let’s modify the code to load and display the subcategories based on this field:

<code>
             &lt;?php
                $currentCat = Mage::registry('current_category');
                if ( $currentCat->getParentId() == Mage::app()->getStore()->getRootCategoryId() )
                {
                    // current category is a toplevel category
                    $loadCategory = $currentCat;
                }
                else
                {
                    // current category is a sub-(or subsub-, etc...)category of a toplevel category
                    // load the parent category of the current category
                    $loadCategory = Mage::getModel('catalog/category')->load($currentCat->getParentId());
                }

                // Load categories with sort order and is active status
                $collection = Mage::getResourceModel('catalog/category_collection')
                            ->addAttributeToFilter('parent_id', $loadCategory->getId())
                            ->addAttributeToFilter('is_active', 1)
                            ->setOrder('sort_order', 'asc');

                foreach ( $collection as $category )
                {
                    if($category->getIsActive())
                    {
                        echo '&lt;a href="'.$category->getURL().'"&gt;'.$category->getName().'&lt;/a&gt;';
                    }
                }
            ?&gt;
</code>

Conclusion

In this post, we explored how to change the sorting order of Magento subcategories from the default (by ID) to the order specified by the admin user.

We modified the existing code to use a collection object and added the sort_order attribute as the sort criterion. This ensures that the subcategories are displayed in the exact sequence set in the admin panel using the drag-and-drop functionality.

Remember, always test your changes thoroughly after making modifications like these, especially if you’re working on a live site.

For more advanced Magento customization tips and best practices, check out our Magento Development guide for detailed insights.

Catalog Admin Orders

Our network