2

How can i get latest products programmatically in magento 2?

1 Answer 1

7

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>";
}
1
  • Thankyou the code is working fine....
    – srg
    Aug 4, 2017 at 9:56

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.