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
What Is Sitebulb and Why SEO Experts Love It?
Sitebulb is a powerful website audit tool that many SEO experts love. It does more than just scan a website it gives a deep a...
What Is Geotagging and How Does It Actually Work?
Geotagging means adding location information, like latitude and longitude, to digital media such as photos, videos, or text m...
GEO Vs AEO Vs SEO Comparison Guide – Revolution of AI Marketers in 2025
We’re entering a new era where AI and human curiosity come together and if your business isn’t ready, you could fall behi...
WordPress Plugin All-in-One WP Migration Security Bypass (2.0.4)
The All-in-One WP Migration plugin for WordPress has a security bypass vulnerability that could let attackers perform restric...
