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
Google Ads Book Buttons Expand To Search & Performance Max
Google is expanding the book button through Reserve with Google partner integration to Google Ads Search and Performance Max ...
OpenAI Allows Health & Finance Ads in ChatGPT: What Marketers Need to Know
The advertising landscape for AI platforms is changing rapidly, and OpenAI’s latest ChatGPT advertising policy updates are ...
How SAGE Can Simplify SEO, AEO and GEO Optimization
Search is changing rapidly. Websites are no longer competing only for traditional Google rankings. With AI-powered search, fe...
What Is HeadingsMap and How Does It Help With SEO?
When optimising a website for search engines, a clear and well-organised heading structure is essential. HeadingsMap is a han...