Skip to content

Instantly share code, notes, and snippets.

@patpohler
Created April 29, 2012 19:07
Show Gist options
  • Save patpohler/2552751 to your computer and use it in GitHub Desktop.
Save patpohler/2552751 to your computer and use it in GitHub Desktop.
HACK! Modification to mod.search.php to support "category='not ...'" in ExpressionEngine's advance search (start at line 1014). USE AT YOUR OWN RISK!
/** ----------------------------------------------
/** Limit query to a specific category
/** ----------------------------------------------*/
// Check for different sets of category IDs, checking the parameters
// first, then the $_POST
$exclude_cat = false;
if (isset($this->_meta['category']) AND $this->_meta['category'] != '' AND ! is_array($this->_meta['category']))
{
if(strpos($this->_meta['category'], "not ") > -1) {
$exclude_cat = true;
$this->_meta['category'] = str_replace("not ", "", $this->_meta['category']);
}
$this->_meta['category'] = explode('|', $this->_meta['category']);
}
else if (
( ! isset($this->_meta['category']) OR $this->_meta['category'] == '') AND
(isset($_POST['cat_id']) AND is_array($_POST['cat_id']))
)
{
$this->_meta['category'] = $_POST['cat_id'];
}
else
{
$this->_meta['category'] = '';
}
if (is_array($this->_meta['category']))
{
$temp = '';
if($exclude_cat) {
foreach ($this->_meta['category'] as $val)
{
if ($val != 'all' AND $val != '')
{
$temp .= " exp_categories.cat_id != '".$this->EE->db->escape_str($val)."' AND";
}
}
if ($temp != '')
{
$temp = substr($temp, 0, -3);
$sql .= ' AND ('.$temp.' OR exp_categories.cat_id IS NULL) ';
}
} else {
foreach ($this->_meta['category'] as $val)
{
if ($val != 'all' AND $val != '')
{
$temp .= " exp_categories.cat_id = '".$this->EE->db->escape_str($val)."' OR";
}
}
if ($temp != '')
{
$temp = substr($temp, 0, -2);
$sql .= ' AND ('.$temp.') ';
}
}
}
/** ----------------------------------------------
/** Are there results?
/** ----------------------------------------------*/
@patpohler
Copy link
Author

This is an UNOFFICIAL, UNSUPPORTED HACK to mod.search.php in ExpressionEngine. USE AT YOUR OWN RISK! I'm not affiliated with EllisLabs, just a dev who needed the ability to exclude categories in search (and still search for uncategorized entries) and felt it was ridiculous to pay for a $99 3rd party module for this basic functionality.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment