'Magento 2 product collection sorting is not working (I am doing with custom attribute)
This is my code
$_productCollection = $block->getLoadedProductCollection()->addAttributeToSort('article_position', 'DESC');
echo '<pre>';
print_r($_productCollection->getdata());
OR
$_productCollection->setOrder('article_position', 'DESC');
echo '<pre>';
print_r($_productCollection->getdata());
But collection data is same no effect in my collection why ?
Solution 1:[1]
Use following code to change sort order of product collection:
$_productCollection->getSelect()
->reset(Zend_Db_Select::ORDER)
->order('article_position', 'DESC');
echo '<pre>';
print_r($_productCollection->getData());
Note "article_position" is an custom attribute
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Amaresh Tiwari |