1

How can i get latest products programmatically in magento 2?

4

try this

$productCollectionFactory = $objectManager->get('\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory');
$collection = $productCollectionFactory->create();
$collection->addAttributeToSelect('*');
$collection->addAttributeToSort('entity_id','desc');
$collection->setPageSize(5); // selecting only 5 products

foreach ($collection as $product) {
    print_r($product->getId());     
    echo "<br>";
}
  • Thankyou the code is working fine.... – srg Aug 4 '17 at 9:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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