{"id":1517,"date":"2021-01-08T10:37:43","date_gmt":"2021-01-08T10:37:43","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/sort-by-newest-product-magento-2\/"},"modified":"2025-07-17T10:11:31","modified_gmt":"2025-07-17T04:41:31","slug":"sort-by-newest-product-magento-2","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/sort-by-newest-product-magento-2\/","title":{"rendered":"How to Add Sort by &#8220;Newest Product&#8221; option in Magento 2"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">It\u2019s never \u2018job done\u2019 when it comes to providing exceptional customer experiences. It is necessary to offer out-of-the-box functionalities to customers for their convenience.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Bringing products with certain criteria to the top of the page can be particularly useful for customers who aren\u2019t exactly sure what they want.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Default\u00a0Magento 2\u00a0provides three options for product sorting on category page.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Product<\/li>\n\n\n\n<li>Position<\/li>\n\n\n\n<li>Name<\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/meetanshi.com\/blog\/wp-content\/uploads\/2019\/07\/1_default-Magento-2-price-sorting-1024x349.png\" alt=\"1_default Magento 2 price sorting\" class=\"wp-image-5909\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">To increase and maintain user efficiency, default sorting options are not enough. We have to make a remarkable effort to present more sorting facilities for customers.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Mostly customers are curious by nature and always affectionate towards browsing the latest products. Today, I\u2019ve come up with the solution to&nbsp;add sort by newest product option in Magento 2.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Steps to Add Sort by&nbsp;<strong>Newest Product&nbsp;<\/strong>Option in Magento 2<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">1 . Create&nbsp;<strong>di.xml<\/strong>&nbsp;file in&nbsp;<strong>Vendor\/Module\/etc<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?xml version=\"1.0\"?>\n&lt;config xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\"\n        xsi:noNamespaceSchemaLocation=\"urn:magento:framework:ObjectManager\/etc\/config.xsd\">\n    &lt;type name=\"Magento\\Catalog\\Model\\Config\">\n        &lt;plugin name=\"Vendor_ModuleName::addCustomOptions\" type=\"Vendor\\Module\\Plugin\\Model\\Config\"\/>\n    &lt;\/type>\n    &lt;type name=\"Magento\\Catalog\\Block\\Product\\ProductList\\Toolbar\">\n        &lt;plugin name=\"catalog_productlist_toolbar_plugin\" type=\"Vendor\\Module\\Plugin\\Product\\ProductList\\Toolbar\"\/>\n    &lt;\/type>\n&lt;\/config><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">2 . Create&nbsp;<strong>Config.php<\/strong>&nbsp;file in&nbsp;<strong>Vendor\/Modulename\/Plugin\/Model<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\n\nnamespace Vendor\\Modulename\\Plugin\\Model;\nuse Magento\\Store\\Model\\StoreManagerInterface;\n\nclass Config\n{\n    protected $_storeManager;\n\n    public function __construct(\n        StoreManagerInterface $storeManager\n    )\n    {\n        $this->_storeManager = $storeManager;\n    }\n\n    public function afterGetAttributeUsedForSortByArray(\\Magento\\Catalog\\Model\\Config $catalogConfig, $options)\n    {\n        $customOption['newest_product'] = __('Newest Product');\n        $options = array_merge($customOption, $options);\n        return $options;\n    }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">3 . Create&nbsp;<strong>Toolbar.php<\/strong>&nbsp;file in&nbsp;<strong>Vendor\/ModuleName\/Plugin\/Product\/ProductList<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">change Vendor\/ModuleName\/Plugin\/Product\/ProductList\/Toolbar.php\n&lt;?php\n\nnamespace Vendor\\ModuleName\\Plugin\\Product\\ProductList;\n\nuse Magento\\Catalog\\Block\\Product\\ProductList\\Toolbar as Productdata;\n\nclass Toolbar\n{\n    public function aroundSetCollection(Productdata $subject, \\Closure $proceed, $collection)\n    {\n        $currentOrder = $subject->getCurrentOrder();\n        if ($currentOrder) {\n            if ($currentOrder == \"newest_product\") {\n                $direction = $subject->getCurrentDirection();\n                $collection->getSelect()->order('created_at ' . $direction);\n            }\n            return $proceed($collection);\n        }\n    }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Following the above steps, you can see the sort by newest product option in Magento 2 category page as shown below:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/meetanshi.com\/blog\/wp-content\/uploads\/2021\/01\/Screenshot-at-December-29th-2020-5.55.59-pm-1024x441.png\" alt=\"add sort by newest product option in Magento 2\" class=\"wp-image-12592\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">If you are using a new version of Magento (2.4.4 onwards), the above method may not work properly. In such cases, follow these steps:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>In&nbsp;<code>eav_attribute<\/code>&nbsp;table, find the&nbsp;<code>created_at<\/code>&nbsp;product attribute and set its&nbsp;<code>frontend_label<\/code>&nbsp;column value to&nbsp;<code>Created At<\/code>.<\/li>\n\n\n\n<li>In the&nbsp;<code>catalog_eav_attribute<\/code>&nbsp;table, find the&nbsp;<code>created_at<\/code>&nbsp;product attribute, and set its&nbsp;<code>used_for_sort_by<\/code>&nbsp;column value to 1.<\/li>\n\n\n\n<li><a href=\"https:\/\/meetanshi.com\/blog\/clear-magento-2-cache\/\" target=\"_blank\" rel=\"noreferrer noopener\">Clear Magento 2 cache<\/a>.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">You can also do this by using the following SQL Query:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># Get the attribute_id of 'created_at'\nselect attribute_id from eav_attribute where attribute_code = 'created_at' and entity_type_id=4;\n\n# Set frontend_label\nupdate eav_attribute set frontend_label = 'Created At' where attribute_id=112;\n\n# Set used_for_sort_by\nupdate catalog_eav_attribute set used_for_sort_by = 1 where attribute_id=112;<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">That\u2019s all!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Also, let me know if you found any positive impact on the purchase of new products after implementing the solution.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Please do consider sharing this post with the Magento Community via social media. Thank You.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>It\u2019s never \u2018job done\u2019 when it comes to providing exceptional customer experiences. It is necessary to offer out-of-the-box functionalities to customers for their convenience. Bringing&#8230;<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[34],"tags":[],"class_list":["post-1517","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1517","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=1517"}],"version-history":[{"count":5,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1517\/revisions"}],"predecessor-version":[{"id":13881,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1517\/revisions\/13881"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=1517"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=1517"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=1517"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}