Magento. Sort products collection by position

Magento. Sort products collection by position

October 19, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento. Sort products collection by position

Sorting Magento Products by Position: A Comprehensive Guide

As an experienced developer working on e-commerce platforms, I often encounter questions about sorting products in Magento. Sorting products by position is a critical aspect of managing your catalog effectively. In this post, we will explore how to achieve this using the correct approach and best practices.

Understanding Product Positions in Magento

Magento allows you to set positions for each product within a category. These positions determine the display order of products on the frontend. By default, the position attribute is an integer that represents the order index. A lower value means higher priority.

To access this information, we can leverage Magento’s built-in collection filtering capabilities. Let me show you how to sort your product collection by position using a simple example:

$categoryId = 3; // Replace with your desired category ID

$_products = Mage::getModel('catalog/product')->getCollection()
    ->addFieldToFilter('category_id', $categoryId)
    ->addAttributeToSort('position', 'asc');

// Print the sorted product collection
foreach ($_products as $_product) {
    echo $_product->getName() . '<br>';
}

In this code, we first create a new product collection and filter it to include only products assigned to the specified category (category_id). Then, we use the addAttributeToSort method to sort the collection by the position attribute in ascending order. The resulting collection is sorted based on the positions you’ve set for each product in the category.

Sorting with Additional Filters

If you need to apply additional filters or sort the products further, you can chain multiple methods together. Here’s an example of sorting by position and then by name:

$_products = Mage::getModel('catalog/product')->getCollection()
    ->addFieldToFilter('category_id', $categoryId)
    ->addAttributeToSort('position', 'asc')
    ->addAttributeToSort('name', 'asc');

In this case, the products are first sorted by position in ascending order. If two or more products share the same position, they will then be sorted alphabetically by name.

Sorting Products with Complex Conditions

For more advanced sorting scenarios that involve multiple conditions, you can use Magento’s query builder to create complex queries efficiently. The addAttributeToFilter and addAttributeToSort methods support various filter conditions like equal to (eq), not equal to (neq), greater than or equal to (gteq), and so on.

For example, let’s sort products by position in descending order within a specific price range:

$_products = Mage::getModel('catalog/product')->getCollection()
    ->addAttributeToFilter('price', array('gteq' => 10, 'lteq' => 50))
    ->addAttributeToSort('position', 'desc');

In this query, we first filter the products by price using the addAttributeToFilter method. We’re looking for products with prices between $10 and $50 (inclusive). Then, we sort these filtered products by position in descending order.

Conclusion

Sorting Magento products by position is a straightforward process when you understand the built-in collection sorting capabilities provided by the platform. By using the addAttributeToSort method correctly, you can easily organize your product catalog based on the positions defined in the category management section of the admin panel.

Remember to use appropriate filter conditions and sorting orders (asc, desc) for your specific business requirements. And always consider performance optimizations when dealing with large collections.

If you’re looking for more advanced Magento development insights, make sure to check out LaravelCompany’s Magento tutorials. We cover a wide range of topics from customizing themes to implementing complex integrations.

Catalog Orders

Our network