Unpublish child pages when unpublishing a parent resource?

Is there a way to automatically unpublish all child pages when unpublishing a parent resource without manually unpublishing each child page?

You could write a plugin for that.

A similar topic was discussed in this thread:

The problem in MODX is, that there isn’t a single event to capture all cases.

The event OnDocUnPublished for example is only invoked, if the resource is unpublished via the context menu in the resource tree.

Your question made me rethink this and come up with two ideas.

I believe this plugin, attached to OnDocUnPublished, would do it if you always remember to unpublish by right-clicking on the resource in the tree (untested):

$publishedChildren = $resource->getMany('Children', array('published' => true));

if (!empty($publishedChildren)) {
    foreach ($publishedChildren as $child) {
        $child->set('published', false);
        $child->save();
    }
}

Another way to go, which would work no matter how the resource was unpublished, would be this similar plugin:

$unpublishedResources = $modx->getCollection('modResource');
if (! empty($unpublishedResources)) {
   foreach ($unpublishedResources as $doc) {
       $publishedChildren = $doc->getMany('Children', array('published' => true));

       if (!empty($publishedChildren)) {
           foreach ($publishedChildren as $child) {
               $child->set('published', false);
               $child->save();
           }
       }
   }
}

That will unpublish all child docs with unpublished parents. What you attach it to would depend on how often it needs to run. The downside is that it’s going to process every unpublished resource on the site whenever it runs.

Just to mention it: This plugin code from @bobray only unpublishes direct children.
If children multiple levels deep in the hierarchy should be unpublished, use the function getChildIds.