Magento product sort order using an attribute

Magento product sort order using an attribute

July 10, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento product sort order using an attribute

Solving Magento Product Sort Order Using an Attribute

Hello Tibin,

I’m glad you’re exploring custom sorting options for your Magento products. Let’s break down your issue step by step.

Understanding the Current Issue

You’ve already added a custom attribute named ‘display_order’ and linked it to the product attribute set. You’ve also manually assigned unique values to this field for each product, which is great. However, you’re currently facing two challenges:

  1. The current filters are sorting products only within similar attribute sets.
  2. The sort order doesn’t fully respect the ‘display_order’ value across all product types.

The Magento Sorting Mechanics

By default, Magento uses a complex algorithm for product sorting that includes factors like stock status, visibility, and category position. When you add your own custom field (like ‘display_order’), it gets layered into this existing hierarchy.

The issue lies in the way you’re filtering and sorting your collections:

$_productCollection = Mage::getModel('catalog/product')->getCollection();
$_productCollection->addAttributeToSelect('*');
$_productCollection->addAttributeToFilter('display_order', array('gt' => 0));

And

$_productCollection = Mage::getModel('catalog/product')->getCollection();
$_productCollection->addAttributeToSelect('*');
$_productCollection->addAttributeToSort('display_order', 'ASC');

These methods are almost correct, but the addAttributeToFilter and addAttributeToSort functions are somewhat limited in their behavior when used together.

The Solution: Reordering the Collection

The key is to first filter your products by the ‘display_order’ attribute, then sort them based on this field. You can achieve this using the setOrder method:

$_productCollection = Mage::getModel('catalog/product')->getCollection();
$_productCollection->addAttributeToSelect('*');
$_productCollection->addFieldToFilter('display_order', array('gt' => 0));
$_productCollection->setOrder('display_order', 'ASC');

// Helper to display products
$_helper = $this->helper('catalog/output');

In this code, addFieldToFilter is used instead of addAttributeToFilter. This ensures the filter applies correctly across all product types. Then, setOrder is called with the ‘display_order’ field and ‘ASC’ as the sort direction.

Advanced Sorting Options

If you want to combine multiple sorting conditions (like display order + stock status), you can use the addAttributeToSort function after setting your main order:

$_productCollection->setOrder('display_order', 'ASC');
$_productCollection->addAttributeToSort('stock_status', 'DESC');

Conclusion

Remember, Magento’s sorting behavior is complex and often requires a layered approach. By combining the addFieldToFilter and setOrder methods, you should now be able to sort your products accurately across all attribute sets based on the ‘display_order’ field.

If you’re still facing issues or need further assistance with Magento development tasks, feel free to explore our Magento Development Services at Laravel Company. We specialize in helping businesses like yours unlock the full potential of their e-commerce platforms.

Keep exploring and happy coding!

Best, [Your Name] Laravel Company

Catalog Orders Errors

Our network