magento2 – Sort order and limit product collection

First Way :

$collection = $this->_products->create()->getCollection();
$collection->addFieldToFilter('is_active', '1')->setOrder('created_at', 'desc');
$collection->setPageSize(3)->setCurPage(1);

Second Way :

$collection->getSelect()->order('enitity_id DESC');

Leave a Reply

Your email address will not be published. Required fields are marked *