Magento: Sort latest product by ‘created date’ and ‘new from date’
Here is a quick tip to sort/order latest product by both created date and new from date.
Created At date is automatically added when you add a new product. However, you have to set New from Date manually while adding or editing product.
You may have product with same new from date. And, then you need created at date to sort the latest product.
Here is the code to sort/order product by both created date and new from date:-
$todayDate = Mage::app()->getLocale()->date()->toString(Varien_Date::DATETIME_INTERNAL_FORMAT);
$collection = Mage::getModel('catalog/product')
->addAttributeToFilter('news_from_date', array('date' => true, 'to' => $todayDate))
->addAttributeToFilter('news_to_date', array('or'=> array(
0 => array('date' => true, 'from' => $todayDate),
1 => array('is' => new Zend_Db_Expr('null')))
// If you want to filter the product collection by enabled and visibility status
// i.e. showing product that are enabled and visible in catalog and search
Hope this helps. Thanks.