Filter Magento admin grid by custom column value

Error / Problem: column not exists.

Developers come across this situation often when they customize / rewrite order grid or product grid.

You would get in the circumstances when you have customized or rewritten the Magento admin grid.
You have added a column with your own column or attribute. 
Magento has a striking ability to filter the grids using custom column.

In the
protected function _prepareColumns()

You'd need to add a array element while adding custom column.

$this->addColumn('CUSTOM_COLUMN_ID', array(
            'header' => Mage::helper(HELPER)->__('CUSTOM COLUMN TITLE'),
            'index' => 'COLUMN_ID',
            'filter_condition_callback' => array($this, '_filterIdCondition'),
));

You'd might noticed filter_condition_callback element used in the array.
the second element of the array _filterIdCondition is the function being called to filter the Magento admin grid.

The _filterIdCondition function would look something like below.

protected function _filterIdCondition($collection, $column) {
        if (!$value = trim($column->getFilter()->getValue())) {
            return;
        }

        $where = ' (COLUMN_ID = '. $value .' )';
        $collection->getSelect()->where(" {$where} ");
}

If the 'where' do not work you can use 'having' like below.

$collection->getSelect()->having(" {$where} ");