Magento 2 – Sort Product Collection by Last Update

magento2product-collection

I am trying to fetch product collection based on crated custom attribute that is is_home_page.

Check by below code.

echo "<pre>";
$productCollection = $objectManager->create('Magento\Catalog\Model\ResourceModel\Product\Collection');
/** Apply filters here */
$collection = $productCollection->addAttributeToSelect('*')
->addAttributeToFilter('show_on_home_page',['eq'=>'1'])
->setPageSize(4)
->setOrder('sort_order','DESC')
->load();

So, here what i need is Currently i want to display products on home page which product has attribute set to Yes.

I can able to get collection of products which has attribute Yes.

But i would like to get collection in order to last updated product.

how can i achieve this ?

Best Answer

This will do for you

 $productCollection->addAttributeToSort('update_at', 'DESC')