Ericc
Ericc

Reputation: 11

Magento: Search CMS Page content for keyword

I'm trying to modify a search function that currently only searches CMS page titles. I want to include a search of the page content. Currently the function is as follows:

public function getSearchCMSPages($keyword){
    $result = array();
    $storeId    = Mage::app()->getStore()->getId();
    $cmspages = Mage::getModel('cms/page')->getCollection()
    ->addFieldToFilter("is_active",1)
    ->addFieldToFilter('title',array('like'=>'%'. $keyword.'%'))
    ->setCurPage(1)
    ->setOrder('title','ASC');
    $cmspages->load();
    if(count($cmspages))
    {
        foreach($cmspages as $_page)
        {
            $result[] = $_page->getId();
        }
    }
    return $result;
}

What I want to do is modify this function to also search the CMS page content for the keyword.

Any help or suggestions on how to tackle this is greatly appreciated.

-Eric

Upvotes: 0

Views: 1471

Answers (1)

Rajeev K Tomy
Rajeev K Tomy

Reputation: 2214

You can try this. It worked for me.

public function getSearchCMSPages($keyword)
{
    $result = array();
    $storeId    = Mage::app()->getStore()->getId();
    $cmspages = Mage::getModel('cms/page')->getCollection()
    ->addFieldToFilter("is_active",1)
    ->addFieldToFilter(
        array('title','content'),
        array(
            array('like'=>'%'. $keyword.'%'),
            array('like'=>'%'. $keyword.'%')
        )
    )
    ->setCurPage(1)
    ->setOrder('title','ASC');
    $cmspages->load();

    if(count($cmspages))
    {
        foreach($cmspages as $_page)
        {
            $result[] = $_page->getTitle();
        }
    }
    return $result;
}

The only difference with your code is, we are now filtering content attribute of CMS Pages along with title attribute as like this

    ->addFieldToFilter(
        array('title','content'),
        array(
            array('like'=>'%'. $keyword.'%'),
            array('like'=>'%'. $keyword.'%')
        )
    )

Rest is same. Hope it helps

Upvotes: 1

Related Questions