private function get_attachments_for_posts(array $groupedposts) : array {
        $attachmentsbypostid = [];
        $postattachmentvault = $this->vaultfactory->get_post_attachment_vault();
        $postsbyforum = array_reduce($groupedposts, function($carry, $grouping) {
            ['forum' => $forum, 'posts' => $posts] = $grouping;

            $forumid = $forum->get_id();
            if (!isset($carry[$forumid])) {
                $carry[$forumid] = [
                    'forum' => $forum,
                    'posts' => []
                ];
            }

            $carry[$forumid]['posts'] = array_merge($carry[$forumid]['posts'], $posts);
            return $carry;
        }, []);

        foreach ($postsbyforum as $grouping) {
            ['forum' => $forum, 'posts' => $posts] = $grouping;
            $attachments = $postattachmentvault->get_attachments_for_posts($forum->get_context(), $posts);

            // Have to loop in order to maintain the correct indexes since they are numeric.
            foreach ($attachments as $postid => $attachment) {
                $attachmentsbypostid[$postid] = $attachment;
            }
        }

        return $attachmentsbypostid;
    }