I'd like to add product id as a new sorting option in frontend catalog pages. How can I do that?

This attribute is not listed in product attributes configuration in magento backend. I guess that it can be done by custom extension but I don't know how. Could you post a solution?

2 Answers 2


What you need to do is follow this link Magento 2 How to add custom sort by option , override this two files as @LucScu mentioned:



and modify the code for your case , instead of created_at add entity_id.

Note ! Do not copy -> paste his code, but change the files based on your magento2 version (he may had a lower magento2 version) .


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

enter image description here

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.

enter image description here

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"/>

After Add Toolbar.php file in your module Vendor\Module\Block\Product\ProductList\Toolbar.php

namespace Vendor\Module\Block\Product\ProductList;

class Toolbar extends \Magento\Catalog\Block\Product\ProductList\Toolbar
    public function setCollection($collection)
              $collection->getSelect()->order("e.entity_id desc");

        $this->_collection = $collection;


        $limit = (int)$this->getLimit();
        if ($limit) {
        if ($this->getCurrentOrder()) {
            $this->_collection->setOrder($this->getCurrentOrder(), $this->getCurrentDirection());
        return $this;

I hope this will help and cover what you are required

  • This could be a solution, but it requires creating a new attribute. I'd like to use en existing attribute, which is product id
    – zekia
    May 23, 2019 at 8:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.