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

**URL:** <https://community.modx.com/t/best-way-to-create-filters-like-newegg-or-b-h-photo/3451>\
**Category:** Development\
**Created:** [January 2, 2021, 4:15am UTC](https://community.modx.com/t/best-way-to-create-filters-like-newegg-or-b-h-photo/3451 "2021-01-02T04:15:43Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![pixelstuff](https://community.modx.com/letter_avatar_proxy/v4/letter/p/ce73a5/32.png) [@pixelstuff](https://community.modx.com/u/pixelstuff)\
**Post date:** [January 2, 2021, 4:15am UTC](https://community.modx.com/t/best-way-to-create-filters-like-newegg-or-b-h-photo/3451/1 "2021-01-02T04:15:43Z")

</div>

What methods would be the best way to create a filtering sidebar similar to what sites like Newegg or B&H Photo have? I was looking at the Taxonomies extra, but the documentation page seems to be missing in action, and I’m not quite sure how to make it return a list of results from check boxes.

For an example of the kind of sidebar I’m talking about:

 ![image](https://modx-community.s3.dualstack.us-east-1.amazonaws.com/original/2X/7/7dc4684d981546ac2e212d6ec66fdc79ac9e52dc.png)

 ![image](https://modx-community.s3.dualstack.us-east-1.amazonaws.com/original/2X/a/a90578e08fa7d3c8832eb05dc8fea0ea7dd6042a.png)

---

<div class="post-metadata">

**Author:** ![halftrainedharry](https://community.modx.com/user_avatar/community.modx.com/halftrainedharry/32/1063_2.png) [@halftrainedharry](https://community.modx.com/u/halftrainedharry)\
**Post date:** [January 2, 2021, 10:34am UTC](https://community.modx.com/t/best-way-to-create-filters-like-newegg-or-b-h-photo/3451/2 "2021-01-02T10:34:31Z")

</div>

> [@pixelstuff](#):
>
> how to make it return a list of results from check boxes

The extra _Taxonomies_ has the snippet `getPagesByTerm` that returns the resources for **one** term only (property `$term_id`). If you want to filter by multiple terms, you have to write your own snippet (or extend the snippet `getPagesByTerm`). You also have to process $\_POST or $\_GET parameters from the frontend yourself. Additionally it seems that `getPagesByTerm` doesn’t support template variables.

Maybe you can use the extra _Tagger_ instead for your purposes (if you don’t need hierarchically nested tags).

---

<div class="post-metadata">

**Author:** ![pixelstuff](https://community.modx.com/letter_avatar_proxy/v4/letter/p/ce73a5/32.png) [@pixelstuff](https://community.modx.com/u/pixelstuff)\
**Post date:** [January 15, 2021, 5:13am UTC](https://community.modx.com/t/best-way-to-create-filters-like-newegg-or-b-h-photo/3451/3 "2021-01-15T05:13:58Z")

</div>

I don’t really need the terms to be hierarchical, but I do need them to be in groups and preferably be predefined in the back end where the creators can check the matching terms rather than make things up on the fly. I liked the discoverability of terms in the backend with Taxonomies. If someone creates a new term in the back end it also needs to update the front end collection of checkboxes.

---

<div class="post-metadata">

**Author:** ![lucy](https://community.modx.com/user_avatar/community.modx.com/lucy/32/43_2.png) [@lucy](https://community.modx.com/u/lucy)\
**Post date:** [January 15, 2021, 5:37pm UTC](https://community.modx.com/t/best-way-to-create-filters-like-newegg-or-b-h-photo/3451/4 "2021-01-15T17:37:39Z")

</div>

Most e-commerce stores use “faceted search”, which is what you have shown in your example photos. This differs from standard filtering by the ability to offer further arrays of filters depending on previous choices. If this is the complexity you’re needing then I think you will need to look at one of the MODX search extras like SimpleSearch or AdvSearch, combined with Tagger (probably). If you do a search for posts on those extras you will find some discussions about this already. And the MODX docs have a short article on SimpleSearch faceted search here:

> **[SimpleSearch.Faceted Search Through PostHooks - SimpleSearch](https://docs.modx.com/current/en/extras/simplesearch/simplesearch/faceted-search-through-posthooks)**

  
If you don’t need the faceted aspect and just want a list of grouped filters, you can use Tagger to do this.

---

<div class="post-metadata">

**Author:** ![pixelstuff](https://community.modx.com/letter_avatar_proxy/v4/letter/p/ce73a5/32.png) [@pixelstuff](https://community.modx.com/u/pixelstuff)\
**Post date:** [January 15, 2021, 10:42pm UTC](https://community.modx.com/t/best-way-to-create-filters-like-newegg-or-b-h-photo/3451/5 "2021-01-15T22:42:22Z")

</div>

I had seen the faceted search example, because I have SimpleSearch setup, however (unless I missed something) I ruled that out because it looks like you have to setup a new PostHook for every filter term you add. If a manager wants to add a new term every coulple weeks while building out the site that becomes too complicated.

On the backend at least, Taxonomies seems to work perfectly like they want because a manager can just go create a new taxonomy term in the resource tree and it automatically shows up on every resource for any creator to checkbox that term.

The problem of course has been getting those same terms to be checkbox filters on the front end for anyone searching for things. And preferably to have it automatic whenever a term is added to the tree. I figured getResources could probably handle that, eventually.

I’m about to take another look at Tagger again to see if it can be clamped down a little to act similarly to Taxomomies

---

<div class="post-metadata">

**Author:** ![lucy](https://community.modx.com/user_avatar/community.modx.com/lucy/32/43_2.png) [@lucy](https://community.modx.com/u/lucy)\
**Post date:** [January 15, 2021, 11:35pm UTC](https://community.modx.com/t/best-way-to-create-filters-like-newegg-or-b-h-photo/3451/6 "2021-01-15T23:35:32Z")

</div>

Not positive, but I believe with any of the tagging extras you will only be able to match one term at a time. So, if you want to be able to allow multiple checked filters then you will need one of the search extras… I think you can get Tagger and AdvSearch working together (I saw an article somewhere), so that might allow the backend ease you want with the frontend requirements. Maybe. Hopefully someone who has implemented this will pop in to help.

---

<div class="post-metadata">

**Author:** ![halftrainedharry](https://community.modx.com/user_avatar/community.modx.com/halftrainedharry/32/1063_2.png) [@halftrainedharry](https://community.modx.com/u/halftrainedharry)\
**Post date:** [January 17, 2021, 3:38pm UTC](https://community.modx.com/t/best-way-to-create-filters-like-newegg-or-b-h-photo/3451/7 "2021-01-17T15:38:27Z")

</div>

> [@lucy](#):
>
> I believe with any of the tagging extras you will only be able to match one term at a time.

With the extra _Tagger_ you can also search for multiple tags. The problem is, that with `TaggerGetResourcesWhere` you either get the resources that match at least one of the specified tags or the resources that match all of them ( `&matchAll=`1` `). But what you probably want, is a behaviour like the one described in this question:

> [@Tagger (or and or)](https://community.modx.com/t/tagger-or-and-or/898):
>
> I have two Tagger groups that populate checkbox groups, as follows: Color Red Green Blue Size Small Medium Large I want to be able to filter a [[!pdoPage]] snippet call using [[!TaggerGetResourcesWhere?]]. However, I need to join both Tagger groups, so that I can filter, for example by: Red or Blue and Small or Large. I’ve tried &matchAll=1

To achieve that, you’ll have to write your own snippet.

---

<div class="post-metadata">

**Author:** ![lucy](https://community.modx.com/user_avatar/community.modx.com/lucy/32/43_2.png) [@lucy](https://community.modx.com/u/lucy)\
**Post date:** [January 17, 2021, 3:59pm UTC](https://community.modx.com/t/best-way-to-create-filters-like-newegg-or-b-h-photo/3451/8 "2021-01-17T15:59:00Z")

</div>

There’s a way, using Tagger, to search for multiple tags from the frontend? As in, render checkboxes that the user can check one or many?

---

<div class="post-metadata">

**Author:** ![halftrainedharry](https://community.modx.com/user_avatar/community.modx.com/halftrainedharry/32/1063_2.png) [@halftrainedharry](https://community.modx.com/u/halftrainedharry)\
**Post date:** [January 17, 2021, 9:58pm UTC](https://community.modx.com/t/best-way-to-create-filters-like-newegg-or-b-h-photo/3451/9 "2021-01-17T21:58:03Z")

</div>

Here is a simple example with _Tagger_ that uses a custom snippet (as a replacement for `TaggerGetResourcesWhere`) to allow for the mixing of AND and OR (as described in my last post).

I created two groups in Tagger:

- Group “color” (Id=1) with the tags “red”, “green” and “blue”
- Group “size” (Id=2) with the tags “small”, “medium” and “large”

**Template**

```auto
[[!SetSubquery]]

<form method="post" action="[[~[[*id]]]]" id="my_filter_form">
    <h3>Color</h3>
    [[TaggerGetTags? &groups=`1` &rowTpl=`tplTagCheckbox`]]
    <h3>Size</h3>
    [[TaggerGetTags? &groups=`2` &rowTpl=`tplTagCheckbox`]]
    <input type="submit" value="Filter data">
</form>
<hr>
[[!getResources? &where=`[[!+filter_subquery]]`
    &parents=`1`
    &depth=`0`
    &limit=`0`
    &tpl=`@INLINE <h2>[[+pagetitle]] ([[+id]])</h2>`
]]

```

Set the property `&parents` to the right value.

**Chunk "tplTagCheckbox"**

```auto
<label for="filter-tag-[[+id]]"><input type="checkbox" id="filter-tag-[[+id]]" name="filter[[+group_id]][]" value="[[+id]]" [[!+filter_tag_ids:FormItIsChecked=`[[+id]]`]]/>[[+tag]]</label>

```

This chunk uses the snippet `FormItIsChecked` from the extra _FormIt_.

**Snippet "SetSubquery"**

```auto
<?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 '';

$group_ids = array(1,2); //The groups to include in the filtering
$all_checked_tags = array();
$where = array();
foreach($group_ids as $group_id){
    if (isset($_POST['filter'.$group_id])) {
        $tag_ids = $_POST['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 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 '';

```

This example uses the id of the tags instead of the alias to keep the code simple.

Similar code could be used for the extra _Taxonomies_. That extra has the database table `tax_page_terms` that it basically the same as the table `modx_tagger_tag_resources` from _Tagger_.

---

<div class="post-metadata">

**Author:** ![pixelstuff](https://community.modx.com/letter_avatar_proxy/v4/letter/p/ce73a5/32.png) [@pixelstuff](https://community.modx.com/u/pixelstuff)\
**Post date:** [March 8, 2022, 6:21am UTC](https://community.modx.com/t/best-way-to-create-filters-like-newegg-or-b-h-photo/3451/10 "2022-03-08T06:21:21Z")

</div>

Is there a way to integrate this with SimpleSearch or alternatively use GetResources (or pdoResources) as a replacement for SimpleSearch so that a manual text search can then be filtered by related tags.

The goal being if you did a manual search first for “shoes”, you then get an adjusted sidebar with related tag groups for additional filtering.  
TYPE: boots, tennis shoes, sandals and  
ACTIVITY: hiking, running, dancing, lounging, etc.  
COLOR: white/gray/black, etc.

Of if someone searched for “shirt”, they might get different tag groups to filter with such as  
TYPE: long sleeve, short sleeve, button up, etc.  
MATERIAL: cotton/polyester/lycra, etc.  
COLOR: blue/red/gray/white/balck, etc.

---

<div class="post-metadata">

**Author:** ![pixelstuff](https://community.modx.com/letter_avatar_proxy/v4/letter/p/ce73a5/32.png) [@pixelstuff](https://community.modx.com/u/pixelstuff)\
**Post date:** [March 11, 2022, 11:09pm UTC](https://community.modx.com/t/best-way-to-create-filters-like-newegg-or-b-h-photo/3451/11 "2022-03-11T23:09:25Z")

</div>

I can’t quite follow the logic in “SetSubquery” to understand what the final &where looks like, but would it be difficult to add a second array inside SetSubquery to accept data from a regular input “text” field and combine that with all the other tag names?

It seems like GetResources should be able to use that to do regular search engine tasks as long as the appropriate TVs were turned on. Unless I’m not understanding what the &where tag is doing.

---

<div class="post-metadata">

**Author:** ![halftrainedharry](https://community.modx.com/user_avatar/community.modx.com/halftrainedharry/32/1063_2.png) [@halftrainedharry](https://community.modx.com/u/halftrainedharry)\
**Post date:** [March 12, 2022, 1:03pm UTC](https://community.modx.com/t/best-way-to-create-filters-like-newegg-or-b-h-photo/3451/12 "2022-03-12T13:03:33Z")

</div>

It should be possible to add this input field to the form

```auto
<input type="text" name="search" value="[[!+search_value]]">

```

and then add this code to the snippet **SetSubquery**

```auto
$search_value = '';
if (isset($_POST['search'])) {
    $search_value = filter_var($_POST['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:content:LIKE" => $search_value_wildcards);
    }
}
$modx->setPlaceholder('search_value', $search_value);

```

to search in _resource fields_ (`pagetitle` and `content` in this example).

For _TVs_ you probably have to use the property `&tvFilters` (or write your own subquery with `"EXISTS ( ...)"`)

* * *

> [@pixelstuff](#):
>
> I can’t quite follow the logic in “SetSubquery” to understand what the final &where looks like

You could add the property `&debug` to the _getResources_ call.

```auto
[[!getResources?
    ...
    &debug=`1`
]]

```

The SQL query then gets written to the error log and you can check how the value of the `where` property is converted to the WHERE-clause of the SQL query.

---

<div class="post-metadata">

**Author:** ![pixelstuff](https://community.modx.com/letter_avatar_proxy/v4/letter/p/ce73a5/32.png) [@pixelstuff](https://community.modx.com/u/pixelstuff)\
**Post date:** [March 12, 2022, 5:22pm UTC](https://community.modx.com/t/best-way-to-create-filters-like-newegg-or-b-h-photo/3451/13 "2022-03-12T17:22:06Z")

</div>

> [@halftrainedharry](#):
>
> For _TVs_ you probably have to use the property `&tvFilters` (or write your own subquery with `"EXISTS ( ...)"`)

That sound like it would be statically built. Can the EXISTS subquery be automatically built based on the TVs listed in the snippet call? Such as:

```auto
[[!getResources?
    ...
    &includeTVs=`author,date,image`
]]

```

But, I also imagine skipping over Image type TVs might be good as well unless the searching system does that automatically.

---

<div class="post-metadata">

**Author:** ![pixelstuff](https://community.modx.com/letter_avatar_proxy/v4/letter/p/ce73a5/32.png) [@pixelstuff](https://community.modx.com/u/pixelstuff)\
**Post date:** [March 12, 2022, 6:25pm UTC](https://community.modx.com/t/best-way-to-create-filters-like-newegg-or-b-h-photo/3451/14 "2022-03-12T18:25:31Z")

</div>

I noticed something else (bug like) just now.

If I use this with pdoPage (or getPage) for pagination of the output, then as soon as another page is clicked on the filters or text search box are cleared. Page 2 and beyond are just pagination of the full list.

---

<div class="post-metadata">

**Author:** ![halftrainedharry](https://community.modx.com/user_avatar/community.modx.com/halftrainedharry/32/1063_2.png) [@halftrainedharry](https://community.modx.com/u/halftrainedharry)\
**Post date:** [March 12, 2022, 8:00pm UTC](https://community.modx.com/t/best-way-to-create-filters-like-newegg-or-b-h-photo/3451/15 "2022-03-12T20:00:42Z")

</div>

You have to change the form method to “get”

```auto
<form method="get" ... >

```

and change `$_POST` in the snippet _SetSubquery_ to `$_GET` so that all the request-parameters get appended to the link for the next page.

---

<div class="post-metadata">

**Author:** ![halftrainedharry](https://community.modx.com/user_avatar/community.modx.com/halftrainedharry/32/1063_2.png) [@halftrainedharry](https://community.modx.com/u/halftrainedharry)\
**Post date:** [March 12, 2022, 8:54pm UTC](https://community.modx.com/t/best-way-to-create-filters-like-newegg-or-b-h-photo/3451/16 "2022-03-12T20:54:47Z")

</div>

> [@pixelstuff](#):
>
> Can the EXISTS subquery be automatically built based on the TVs listed in the snippet call?

Only if you change the code of the _getResources_ snippet.

* * *

It turns out that the `&tvFilters` property doesn’t work, unless you only want to search in TVs (and not in form fields as well).

So I believe you have to write a complicated where clause like this to make it work.  
And then add `OR EXISTS (...)` for any additional TV.

```auto
$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 . "%";
        //searches in pagetitle, content and the author TV.
        $where[] = "(`modResource`.`pagetitle` LIKE '" . $search_value_wildcards . "' OR `modResource`.`content` LIKE '" . $search_value_wildcards . "' OR EXISTS (SELECT 1 FROM `modx_site_tmplvar_contentvalues` tvr JOIN `modx_site_tmplvars` tv ON tvr.value LIKE '" . $search_value_wildcards . "' AND tv.name = 'author' AND tv.id = tvr.tmplvarid WHERE tvr.contentid = modResource.id))";
    }
}
$modx->setPlaceholder('search_value', $search_value);

```

---

<div class="post-metadata">

**Author:** ![halftrainedharry](https://community.modx.com/user_avatar/community.modx.com/halftrainedharry/32/1063_2.png) [@halftrainedharry](https://community.modx.com/u/halftrainedharry)\
**Post date:** [March 12, 2022, 9:08pm UTC](https://community.modx.com/t/best-way-to-create-filters-like-newegg-or-b-h-photo/3451/17 "2022-03-12T21:08:05Z")

</div>

There is a paid search extra **mSearch2** that has a built-in filter functionality (snippet `mFilter2`).  
I never used it myself, but it’s likely a cleaner solution than using _getResources_.

> **[mSearch2](https://en.modstore.pro/packages/ecommerce/msearch2)**
>
> $ 33.92, Илья Уткин: Morphological search and ajax filter

* * *

Also the extra [AdvSearch](https://modx.com/extras/package/advsearch) has a property `queryHook`, that probably can be used to query the tagger tags.

---

<div class="post-metadata">

**Author:** ![pixelstuff](https://community.modx.com/letter_avatar_proxy/v4/letter/p/ce73a5/32.png) [@pixelstuff](https://community.modx.com/u/pixelstuff)\
**Post date:** [March 12, 2022, 9:08pm UTC](https://community.modx.com/t/best-way-to-create-filters-like-newegg-or-b-h-photo/3451/18 "2022-03-12T21:08:19Z")

</div>

Although still hard coded into SetSubquery, it looks like when pdoPage or pdoResources have the TVs initialized like so:

```auto
[[!pdoPage?
    ...
    &includeTVs=`author,subtitle,series,date,image`
    &processTVs=`1`
]]

```

(which I needed for proper display of the output)

Then the TVs are searchable just like any resource field. I added them to the end of the where line and it seems to work fine.

```auto
$search_value = '';
if (isset($_POST['search'])) {
    $search_value = filter_var($_POST['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:author:LIKE" => $search_value_wildcards, "OR:subtitle:LIKE" => $search_value_wildcards, "OR:series:LIKE" => $search_value_wildcards);
    }
}
$modx->setPlaceholder('search_value', $search_value);

```

---

<div class="post-metadata">

**Author:** ![pixelstuff](https://community.modx.com/letter_avatar_proxy/v4/letter/p/ce73a5/32.png) [@pixelstuff](https://community.modx.com/u/pixelstuff)\
**Post date:** [October 12, 2022, 5:34pm UTC](https://community.modx.com/t/best-way-to-create-filters-like-newegg-or-b-h-photo/3451/19 "2022-10-12T17:34:24Z")

</div>

It seems that after upgrading to MODx 3.0.1 this doesn’t search anymore. Simplesearch was also broken, but was fixed by updating the search driver URL.

Any idea why this might have broken?

The browser URL does update when a search or filter is applied, but the results don’t update.

---

<div class="post-metadata">

**Author:** ![pixelstuff](https://community.modx.com/letter_avatar_proxy/v4/letter/p/ce73a5/32.png) [@pixelstuff](https://community.modx.com/u/pixelstuff)\
**Post date:** [October 13, 2022, 8:25pm UTC](https://community.modx.com/t/best-way-to-create-filters-like-newegg-or-b-h-photo/3451/20 "2022-10-13T20:25:05Z")

</div>

This is the final code of the snippet.

```auto
<?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

[Next page](https://community.modx.com/t/best-way-to-create-filters-like-newegg-or-b-h-photo/3451.md?page=2)
