Best way to create filters like Newegg or B&H Photo

This is the final code of the snippet.

<?php
$tagger = $modx->getService('tagger','Tagger',$modx->getOption('tagger.core_path',null,$modx->getOption('core_path').'components/tagger/').'model/tagger/',$scriptProperties);
if (!($tagger instanceof Tagger)) return '';

//This section searches based on Tagger words
$group_ids = array(1,2,3,4,5); //The groups to include in the filtering
$all_checked_tags = array();
$where = array();
foreach($group_ids as $group_id){
    if (isset($_GET['filter'.$group_id])) {
        $tag_ids = $_GET['filter'.$group_id];
        if (is_array($tag_ids)){
            $tag_ids = array_map('intval', $tag_ids);
            $where[] = "EXISTS (SELECT 1 FROM {$modx->getTableName('TaggerTagResource')} r WHERE r.tag IN (" . implode(',',$tag_ids) . ") AND r.resource = modResource.id)";
            $all_checked_tags = array_merge($all_checked_tags,$tag_ids);
        }
    }
}
//This section performs a regular text search
$search_value = '';
if (isset($_GET['search'])) {
    $search_value = filter_var($_GET['search'], FILTER_SANITIZE_STRING);
    $min_length_search_value = 3;
    if (mb_strlen($search_value) >= $min_length_search_value){
        $search_value_wildcards = "%" . $search_value . "%";
        $where[] = array("pagetitle:LIKE" => $search_value_wildcards, "OR:tv_title:LIKE" => $search_value_wildcards, "OR:tv_subtitle:LIKE" => $search_value_wildcards, "OR:tv_description:LIKE" => $search_value_wildcards, "OR:tv_author:LIKE" => $search_value_wildcards, "OR:tv_reference:LIKE" => $search_value_wildcards);
    }
}
$modx->setPlaceholder('search_value', $search_value);

//This placeholder is used to preserve the "checked"-state of the checkboxes
$modx->setPlaceholder('filter_tag_ids',$modx->toJson($all_checked_tags));
//This placeholder is used for the getResources subquery
$modx->setPlaceholder('filter_subquery',$modx->toJSON($where));
return '';

I believe in MODx 3, Tagger has also been updated in someway although I don’t know if that would affect the function of the search.

Tagger 2.0.0

  • Add support for Revolution 3.0.0
  • Remove group placement above/under content