We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

Search module hack (EE 1.6.9)

Development and Programming

Mark Croxton's avatar
Mark Croxton
319 posts
15 years ago
Mark Croxton's avatar Mark Croxton

If you need the search module to return entries that belong to ALL of the selected categories in the advanced search form (rather than the default which selects entries in ANY of the selected categories), change this at around line 952:

/** ----------------------------------------------
        /**  Limit query to a specific category
        /** ----------------------------------------------*/       
        if (isset($_POST['cat_id']) AND is_array($_POST['cat_id']))
        {        
            $temp = '';
        
            foreach ($_POST['cat_id'] as $val)
            {
                if ($val != 'all' AND $val != '')
                {
                    $temp .= " exp_categories.cat_id = '".$DB->escape_str($val)."' OR";
                }
            } 
            
            if ($temp != '')
            {
                $temp = substr($temp, 0, -2);
            
                $sql .= ' AND ('.$temp.') ';
            }
        }

to:

/** ----------------------------------------------
        /**  Categories
        /** ----------------------------------------------*/
        if (isset($_POST['cat_id']) AND is_array($_POST['cat_id']))
        {     
            if ($_POST['cat_id'][0] != 'all' && $_POST['cat_id'][0] != '')
            {
                // Find entries that belong to ALL selected categories
                $cat_sql = "SELECT
                            DISTINCT(exp_weblog_titles.entry_id)
                            FROM exp_weblog_titles
                            LEFT JOIN exp_weblogs ON exp_weblog_titles.weblog_id = exp_weblogs.weblog_id 
                            LEFT JOIN exp_category_posts ON exp_weblog_titles.entry_id = exp_category_posts.entry_id
                            WHERE exp_weblogs.site_id = '".$DB->escape_str($PREFS->ini('site_id'))."'";
            
                // make safe            
                foreach($_POST['cat_id'] as &$val)
                {
                    $DB->escape_str($val);
                }
                $selected_categories = "'".implode("','", $_POST['cat_id'])."'";    
                $cat_sql .= "AND exp_category_posts.cat_id IN ({$selected_categories}) 
                         GROUP BY exp_category_posts.entry_id 
                         HAVING (COUNT(exp_category_posts.entry_id) = ".count($_POST['cat_id']).") ";
                    
                $query = $DB->query($cat_sql);
            
                if ($query->num_rows == 0)
                {
                    return FALSE;
                }
                else
                {
                    // add where condition to main query
                    $sql .= "AND exp_weblog_titles.entry_id IN(";
                
                    foreach ($query->result as $row)
                    {
                        $sql .= "'".$DB->escape_str($row['entry_id'])."', ";
                    } 
                
                    $sql = substr($sql, 0, -2).")";    
                }
            }
        }
       

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.