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') ->getCollection() ->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'))) ), 'left') ->addAttributeToSort('news_from_date', 'desc') ->addAttributeToSort('created_at', 'desc');
Hope this code will help you. Thanks.
Recent Posts
Digital Marketing Strategies To Consider In 2025
As we enter 2025, digital marketing is growing quickly, shaped by new technologies and changing customer habits. To stay comp...
How Does a Professional Website Drive Business Success in Today’s Digital Era?
In today’s digital world, having a professional website is more than just being online, it’s a key tool for business succ...
Is It Still Worth Adding Hashtags to Your Posts?
In the fast-changing world of social media, hashtags have been a popular way to increase visibility and engagement. But with ...
Reddit Launches ‘Reddit Answers’ AI Overviews
Reddit has launched “Reddit Answers” which is effectively same as Google’s AI overviews. With Reddit Answers, Redditors...