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
Social Media Calendar 2027 India: Important Dates, Festivals and Marketing Ideas
Social media has become an essential part of business growth, brand awareness and customer engagement. However, maintaining a...
WhatsApp introduces a new shortcut to share Reels in chats
Users who have linked their WhatsApp account to Accounts Center can quickly share Instagram Reels using the chat attachment o...
Why Businesses Need SEO Friendly Websites for Long Term Online Growth
The significance of a website goes beyond merely being the online presence of a business in the current internet age. A websi...
Now You Can Invite Anyone to a WhatsApp Call: No Account Needed, No Cost, No Time Limit
WhatsApp is making online communication easier with its new guest calling feature, allowing users to invite anyone to join a ...