Blog Sections Open

Limiting AjaxSearch Results to a Selected Category

If AjaxSearch falls back to the whole site when a category has no direct keyword hits, the category constraint needs to stay part of the query even in the empty-result path.

A category selector wrapped around AjaxSearch is a common pattern, but it can fail in a subtle way: if the selected category has no direct matches, the search may ignore the category restriction and return results from everywhere.

Typical setup

$parent = $_POST['categorySearch'] != 0 ? $_POST['categorySearch'] : '2';
$parent = 'in:' . $parent;
$result = $modx->runSnippet('AjaxSearch', array(
    'parents' => $parent
));

What to verify

Make sure the category restriction survives the full request path, including the branch where no direct results are found. Otherwise the snippet may silently broaden the search scope.

Recommendation

Search filters should fail narrowly, not broadly. If a category yields no matches, show no matches for that category instead of leaking in unrelated results.

Newer post

Fixing Resource Creation Permissions in a Restricted Manager Role

If a manager can edit existing resources but cannot create children inside a container, the issue is usually the resource policy type, not the role rank alone.

Older post

Centering a Watermark in EvoGallery

Watermarks are easy when the position presets match your design, but centered overlays require understanding how the gallery tool interprets placement codes.