Magento 1.9 sort collection by one of the fields value
-
17-02-2021 - |
Question
Whetever I do it still gives me the same results. Querying cms_block ids:
$collection = Mage::getModel( 'cms/block' )
->getCollection()
// can't add addAttributeToSelect(whatever name here or esterisk because it is casing ..error)
->addFieldToFilter( 'is_active', 1 )
->addFieldToFilter( 'is_slide', 1 )
;
$collection->getSelect()
->order('position ASC');
Tried various sort methods but ... no luck. Any ideas how to get cms/block ids sorted by position ?
Solution
Magento 1.9.x CMS Static Blocks are saved in cms_block table which doesn't have any field called "position". So you cannot sort by "position". Secondly the arguments for setOrder method is
setOrder('field_name','sortdirection')
Hope this helps. Just presented a working code which filters the CMS Blocks by active status and order by name (title) in descending order.
$cmsBlockCollection = Mage::getModel('cms/block')->getCollection();
$cmsBlockCollection->addFieldToFilter('is_active',1);
$cmsBlockCollection->setOrder('title','DESC');
or you can also order the collection by this way
$cmsBlockCollection->getSelect()->order('title','DESC');
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange