Magento – addAttributeToSort() not working with flat product data

flat-catalog

We have a bit of code that looks like this:

$products = Mage::getModel('catalog/product')->getCollection();
$products->addAttributeToSelect('name');
$products->addAttributeToSelect('sellbydate');
$products->addAttributeToSort('sellbydate','ASC');

This has been working fine, but after enabling flat product data the items are no longer sorted. I have reindexed but there is no change.

Should I be using a different function?

Best Answer

The function is correct and checks to see if the catalog is in flat mode. So do not worry about that. Make sure that your attribute is set correctly so that there is a column for it on the flat table.

I think setting used_in_product_listing to yes via a set-up script or via the admin, then preform a reindex should solve this problem.

Related Topic