Sorting magento products collection according to custom attribute

Sorting magento products collection according to custom attribute

July 30, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Sorting magento products collection according to custom attribute

Sorting Magento Products Collection According to Custom Attribute

Sorting products based on custom attributes is a common requirement in Magento development. However, it’s not always straightforward, especially when dealing with numeric values like sort_order_product.

Understanding the Current Issue

The provided code attempts to fetch products and order them by the sort_order_product attribute in ascending order:

$collection = Mage::getModel('catalog/product')
             ->getCollection()
             ->joinField('category_id', 'catalog/category_product', 'category_id', 'product_id = entity_id', null, 'left')
             ->addAttributeToSelect('*')
             ->addAttributeToFilter('category_id', array('in' => $categoryIds))
             ->setOrder('sort_order_product', 'ASC');

However, as mentioned in the question, this approach doesn’t produce the expected results.

Identifying the Mistake

The issue lies in how Magento handles sorting by attributes that are not indexed. By default, sort_order_product is likely not indexed, which means Magento cannot efficiently sort products based on this attribute directly from the collection.

The Solution: Indexing the Custom Attribute

To resolve this issue, you need to index the sort_order_product attribute:

  1. Go to System > Configuration > Catalog > Product Attributes.
  2. Find your sort_order_product attribute in the list.
  3. Set “Visible on Product View Page on Front-end” and “Used for Sorting in Product Listing” to Yes.
  4. Save the configuration.

Fetching Products After Indexing

After indexing, you can fetch the products sorted by the custom attribute:

$collection = Mage::getModel('catalog/product')
             ->getCollection()
             ->addAttributeToSelect('*')
             ->addFieldToFilter('category_id', array('in' => $categoryIds))
             ->setOrder('sort_order_product', 'ASC');

Note the changes:

  • The join has been removed as it’s no longer necessary for sorting.
  • addAttributeToSelect is used to select all attributes, including the custom one.

Verifying the Results

After running this code and displaying the products on your frontend or exporting them, you should see the correct sort order based on the values in the sort_order_product attribute.

Remember that indexing can take some time to update, especially for large product catalogs. You might need to clear Magento’s cache after updating the index configuration.

Conclusion

Sorting Magento products by custom attributes requires proper indexing of those attributes. Always ensure your custom sorting attributes are indexed correctly to achieve efficient and accurate results when fetching and displaying products in the frontend or backend.

For more advanced sorting techniques and performance optimizations, consider reading our guide on Improving Magento Product Listing Performance on LaravelCompany’s blog.

Catalog Indexing Configuration Orders

Our network