Sort Product Collection by Date Added to Category in Magento 2

magento2product-collection

From a category page, I grab the product collection from the category object like so

$productCollection = $category->getProductCollection()->addAttributeToSelect('*');

However, the results are sorted in the reverse order of how they appear on the category page. The products on the category page are sorted in the same way they are in the Magento admin (which I assume is simply 'date added to category').

Is there a way I can use

$productCollection->setOrder() 

In a way that returns my collection in the same order as they appear on the category page?

Best Answer

You can filter product collection by updated date or create date DESC/ASC order.

$productCollection->addAttributeToSort('update_at', 'ASC');
$productCollection->addAttributeToSort('created_at', 'DESC');
Related Topic