How to skip search results from an unpublished parent with SimpleSearch

In a website with many pages, each built from blocks (resources, not folders) with child resources, I want to prevent SimpleSearch returning results from child resources of unpublished blocks and unpublished pages, so the editor does not have to unpublish the child resources as well.
How could this be achieved?

One possible way to do this, is to use the &exclude property of the SimpleSearch snippet and provide a comma-separated list of resource IDs you want to exclude.

[[!SimpleSearch? &exclude=`[[getChildrenIdsWithUnpublishedParent]]` ...]]

You could generate this list with a custom snippet.
Here is an (untested) snippet that might work:

Snippet getChildrenIdsWithUnpublishedParent

$q = $modx->newQuery('modResource');
    'Parent.published' => 0
$q->select($modx->getSelectColumns('modResource', 'modResource', '', array('id')));
$ids = $q->stmt->fetchAll(PDO::FETCH_COLUMN, 0);
return implode(',',$ids);
1 Like

That’s smart :slight_smile: thank you. Would this work for an unpublished parent multiple levels up as well?
I mean a resource could be in an unpublished block(resource) or in a published block that is a child of an unpublished page or even an unpublished folder with pages further up the tree.

maybe publish/unpublish all child resources with a plugin at onDocFormSave?

1 Like

This exact solution only works for direct children of an unpublished parent.

If you have deeper hierarchies, then it is probably easier to use the $ids property to restrict the search to a generated list of resource-ids and set the property $idType to documents.

[[!SimpleSearch? &ids=`[[getResourceIdsForSearch]]` &idType=`documents` ...]]

Then write a snippet getResourceIdsForSearch that traverses the resources tree and excludes every branch with an unpublished parent-node.

1 Like

This topic was automatically closed 2 days after discussion ended and a solution was marked. New replies are no longer allowed. You can open a new topic by clicking the link icon below the original post or solution and selecting “+ New Topic”.