You can do that, by making another custom product attribute latest_products
and it's label of your choice which you want to show on category page sort by
. Don't assign it on any Attribute Set
so it will not show against each product edit admin page.
Make sure you set the Used in Product listing
to Yes
in your newly made custom attribute
After you created the attribute, Go to categories in admin select your desired category and Go to Display setting
and check newly created attribute is selected in Available Product listing Sort By
.
In code, Add di.xml
in Vendor/Module/etc/frontend
in your custom module
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Magento\Catalog\Block\Product\ProductList\Toolbar" type="Vendor\Module\Block\Product\ProductList\Toolbar"/>
</config>
After Add Toolbar.php
file in your module Vendor\Module\Block\Product\ProductList\Toolbar.php
<?php
namespace Vendor\Module\Block\Product\ProductList;
class Toolbar extends \Magento\Catalog\Block\Product\ProductList\Toolbar
{
public function setCollection($collection)
{
if($this->getCurrentOrder()=="latest_products")
{
$collection->getSelect()->order("e.entity_id desc");
}
$this->_collection = $collection;
$this->_collection->setCurPage($this->getCurrentPage());
$limit = (int)$this->getLimit();
if ($limit) {
$this->_collection->setPageSize($limit);
}
if ($this->getCurrentOrder()) {
$this->_collection->setOrder($this->getCurrentOrder(), $this->getCurrentDirection());
}
return $this;
}
}
I hope this will help and cover what you are required