0

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 ?

1

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');
  • $collection = Mage::getModel( 'cms/block' ) ->getCollection() //->addAttributeToSelect('position') ->addFieldToFilter( 'is_active', 1 ) ->addFieldToFilter( 'is_slide', 1 ) ; $collection->setOrder('position','DESC'); What's the difference ? – Rob D. A. Feb 5 '19 at 13:10
  • Regardless if ASC or DESC it gives me the same order Array ( [0] => 2 [1] => 3 [2] => 6 [3] => 7 [4] => 9 [5] => 10 [6] => 12 [7] => 13 [8] => 15 [9] => 16 [10] => 17 [11] => 22 [12] => 24 [13] => 26 [14] => 27 [15] => 28 [16] => 29 [17] => 32 [18] => 34 [19] => 36 [20] => 42 [21] => 47 ) – Rob D. A. Feb 5 '19 at 13:13
  • There is a significant difference, CMS static block collection is a non-eav so its recommended to use addFieldToSelect or addFieldToFilter when you deal with non-eav collection, because it has only fields not attributes. Whereas EAV entities like <product>, <category>, <order>, <customer> data stored in EAV tables, which has attributes so its recommended to use addAttributeToFilter or addAttributeToSort methods when you deal with EAV collection. – Haijerome Feb 5 '19 at 13:14
  • However, with or without addAttribute, it just won't geive me back array in order I want. Assume that the best and the easiest way will be just sql query instead of d..b Magento collection method? – Rob D. A. Feb 5 '19 at 13:18
  • 1
    Thank you Haijerome, all sorted. I'm just rebuilding slide system ( Ultimo - which is in my opinion a little bit .... no good/overcomplicated :) ) now it's simple and very easy to use even for the people who know nothing about how to add image to the block. Thanx again – Rob D. A. Feb 5 '19 at 14:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.