This should do the trick
public function getProducts()
{
/** @var $collection \Magento\Catalog\Model\ResourceModel\Product\Collection */
$collection = $this->_productCollectionFactory->create();
$collection->addAttributeToSelect('*');
$collection->addAttributeToFilter('type_id', \Magento\Catalog\Model\Product\Type::TYPE_SIMPLE);
$collection->getSelect()->order('created_at', \Magento\Framework\DB\Select::SQL_DESC);
$collection->getSelect()->limit(10);
return $collection;
}
In reference to our discussion from below, copy and paste this exactly as you see. The strange thing is that when I pasted your code into my IDE, it was going nuts with errors saying that it didn't recognize certain characters in your type hint for the product collection factory. It's strange but when I typed it out by hand, all errors went away. Copy and paste exactly as you see:
<?php
namespace Packt\HelloWorld\Block;
use Magento\Framework\View\Element\Template;
class Newproducts extends Template
{
protected $_productCollectionFactory;
public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
array $data = []
)
{
$this->_productCollectionFactory = $productCollectionFactory;
parent::__construct($context, $data);
}
public function getProducts()
{
/** @var $collection \Magento\Catalog\Model\ResourceModel\Product\Collection */
$collection = $this->_productCollectionFactory->create();
$collection->addAttributeToSelect('*');
$collection->addAttributeToFilter('type_id', \Magento\Catalog\Model\Product\Type::TYPE_SIMPLE);
$collection->getSelect()->order('created_at', \Magento\Framework\DB\Select::SQL_DESC);
$collection->getSelect()->limit(10);
return $collection;
}
}