Get collection of latest product in Magento2

Sometimes in our extension we need the collection of latest added product. So below is the code through which you get the latest updated videos:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$productCollection = $objectManager->create('Magento\Catalog\Model\ResourceModel\Product\CollectionFactory');
$collection = $productCollection->create()
->addAttributeToSort('created_at', 'DESC')

foreach ($collection as $product) :

echo $product->getName();


Hope this will help you. If you have another solution please mention in comment box. If you need any other help in coding please comment below.

Leave a Reply

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