Hello,
I am using the SimpleSearch Plugin for a search function which lets users search for terms across the entire website.
However, I need to exclude resources that use certain templates (and in some cases resources with a specific template variable set to “true”) from the search results. This is because those resources only exist to be displayed on certain overview pages, but do not actually have their own page (for example, I have a template for team members, each team member is displayed on the “team” page, but does not have an individual page - so if a user searches for the name of a team member and clicks on the result, they end up on an empty page).
The obvious solution would be to simply disable the “searchable” setting on each resource - this does work, but it relies on the editors remembering to disable this each time they create a new resource, so I’d like to find a more elegant solution.
I have found an old forum post which seems to offer a solution: How to limit search results to a particular Template with SimpleSearch? | MODX Community Forums
However, I’m getting a 500 error using that snippet.
<?php
$template_ids = explode(',', $modx->getOption('template_ids', $scriptProperties, 0));
$mode = $modx->getOption('mode', $scriptProperties, 'show'); //show or hide resources with given templates
$c = $modx->newQuery('modResource');
$c->where(array('template:IN' => $template_ids));
if ($collection = $modx->getCollection('modResource')) {
foreach ($collection as $object) {
$ids[] = $object->get('id');
}
}
if ($mode == 'hide') {
$c = $modx->newQuery('modResource');
$c->select($modx->getSelectColumns('modResource', 'modResource', array('id')));
$rows = array();
$resources = array();
if ($c->stmt->execute()) {
if ($results = $c->stmt->fetchAll(PDO::FETCH_ASSOC)) {
$rows = $results;
}
}
foreach ($rows as $row){
if (isset($row['id'])){
$resources[] = $row['id'];
}
}
$scriptProperties['ids'] = implode(',', $resources);
$scriptProperties['exclude'] = implode(',', $ids);
} else {
$scriptProperties['ids'] = implode(',', $ids);
}
return $modx->runSnippet('SimpleSearch', $scriptProperties);
[[!filterSearchResults?
&template_ids=`7`
&idType=`documents`
&mode=`hide`
]]
This would also only solve half of my problem, as I’d still need a way to filter by template variable…
I also tried to use GetResources, like this:
[[!getResources?
&parents=`10,127`
&toPlaceholder=`excludedIdsTeam`
]]
[[!SimpleSearch? &exclude=`[[+excludedIdsTeam]]`]]
This did not work, presumably because I need to get the resources’ IDs instead, but I have not yet figured out how I can do that.
Any advice on how I could get this to work?
EDIT: I’m using MODX 3.0.4-pl and SimpleSearch 3.0.0-pl.