In your extend.php between return [ and ];:

    (new Extend\Event)
        // Add a user to a specific group after number of approved posts, requires flarum/approval.
        ->listen(\Flarum\Post\Event\Posted::class, function (\Flarum\Post\Event\Posted $event) {
            $needsApprovedPosts = 5; // number of approved posts to add group to user
            $addGroup = 5; // use the Id of the group you want to add

            $user = $event->post->user;
            // Skip if user is already part of group
            if ($user->groups->find($addGroup) !== null) return;

            if ($user->posts()->where('is_approved')->count() >= $needsApprovedPosts) {
                $user->groups()->attach($addGroup);
            }
        }),

Update $needsApprovedPosts for the amount of posts the user needs before getting the group acccess.
Update $addGroup to the group Id of the group you want to give access to.

This is untested code, if it errors, just remove the lines from the extend.php.

    luceos I would add ->whereNull('hidden_at') because when a moderator deletes a post under approval from the flag, is_approved is set to 1.

      clarkwinkelmann can I chain where statements? Like e.g.:

      if ($user->posts()->where('is_approved')->whereNull('hidden_at')->count() >= $needsApprovedPosts)

      Another question, I also want to skip mods and admins, how should I modify the following check:

      if ($user->groups->find($addGroup) !== null) return;

      Is there something like a IN statement, e.g.:

      if ($user->groups->in($addGroup, $adminGroup, $modGroup)) return;

      Or should I just use an OR operator:

      if ($user->groups->find($addGroup) !== null 
          || $user->groups->find($adminGroup) !== null
          || $user->groups->find($modGroup) !== null
      ) return;

      So, I managed to make it working using the code from @luceos and the addition of @clarkwinkelmann and a small edit. Thank you guys! 🍻

      Here's the working code:

          (new Extend\Event)
              // Add a user to a specific group after number of approved posts, requires flarum/approval.
              ->listen(\Flarum\Post\Event\Posted::class, function (\Flarum\Post\Event\Posted $event) {
                  $needsApprovedPosts = 2; // number of approved posts to add group to user
                  $addGroup = 6; // use the Id of the group you want to add
                  $adminGroup = 1;
                  $modGroup = 4;
      
                  $user = $event->post->user;
                  // Skip if user is already part of group
                  if ($user->groups->find($addGroup) !== null) return;
                  if ($user->groups->find($adminGroup) !== null) return;
                  if ($user->groups->find($modGroup) !== null) return;
      
                  if ($user->posts()
                            ->where('is_approved', 1)
                            ->whereNull('hidden_at')
                            ->count() >= $needsApprovedPosts) {
                      $user->groups()->attach($addGroup);
                  }
              }),

      The slight difference with the original code @luceos suggested is that ->where('is_approved') doesn't work and needs to be replaced with ->where('is_approved', 1), I guess the Laravel framework doesn't automatically interpret the int as boolean.

      Please let me know if there's a better way to check whether the user groups match any of the three given ones, I couldn't make it working with a single find() statement and an array of ID-s and I'm not sure if three separate find() statements are the optimal way but I'm a Java devloper and I've used SQL more than 10 years ago, so really guessing here.

        4 months later

        CyberGene out of curiosity can this be adapted to add to more than one set of users?

        Aka if user has more than 30 post place in X Group, if user has more than 150 post place in Y Group and remove from X.

        As I’m having a lot of issues with AutoModerator, with it randomly assigning groups and removing them even though they meet the criteria.

          LiamKenyon sure, you can use multiple if () {} else if () {} else if… statements. If you’re not comfortable with it, I will check what the exact PHP syntax is, I’m a Java dev 🙂

            CyberGene I did have an attempt and managed to mess everything up lol.

            Automod keeps removing people even though they meet the criteria, so i used the example above before automod and it worked fine, just need to adapt it so it adds to a X group after X posts, then when they reach Y posts they are removed from X group and added to Y group.

              LiamKenyon with the clarification I am not a PHP developer and I haven't tested what I am posting, from a purely programmers point of view I imagine the code being something like this:

                  (new Extend\Event)
                      // Add a user to a specific group after number of approved posts, requires flarum/approval.
                      ->listen(\Flarum\Post\Event\Posted::class, function (\Flarum\Post\Event\Posted $event) {
                          $needsApprovedPosts1 = 2; // number of approved posts to add group1 to user
                          $needsApprovedPosts2 = 10; // number of approved posts to add group2 to user
                          $needsApprovedPosts3 = 50; // number of approved posts to add group3 to user
                          
                          $addGroup1 = 6; // use the Id of the group1 you want to add
                          $addGroup2 = 7; // use the Id of the group2 you want to add
                          $addGroup3 = 8; // use the Id of the group3 you want to add
                          
                          $adminGroup = 1;
                          $modGroup = 4;
              
                          $user = $event->post->user;
                          // Skip if user is already part of group
                          if ($user->groups->find($addGroup1) !== null) return;
                          if ($user->groups->find($addGroup2) !== null) return;
                          if ($user->groups->find($addGroup3) !== null) return;
                          
                          if ($user->groups->find($adminGroup) !== null) return;
                          if ($user->groups->find($modGroup) !== null) return;
                          
                          $postCount = $user->posts()
                                            ->where('is_approved', 1)
                                            ->whereNull('hidden_at')
                                            ->count();
                           
                          if ($postCount >= $needsApprovedPosts1 && $postCount < $needsApprovedPosts2) {
                              $user->groups()->attach($addGroup1);
                          } elseif ($postCount >= $needsApprovedPosts2 && $postCount < $needsApprovedPosts3) {
                              $user->groups()->attach($addGroup2);
                          } elseif ($postCount >= $needsApprovedPosts3) {
                              $user->groups()->attach($addGroup3);
                          }
                      }),

                I also want an extension like this and I can pay it

                CyberGene

                This was my hacky solution which I "think" worked but seemed a little messy lol as had two extend events.

                (new Extend\Event)
                        // Add a user to a specific group after number of approved posts, requires flarum/approval.
                        ->listen(\Flarum\Post\Event\Posted::class, function (\Flarum\Post\Event\Posted $event) {
                            $needsApprovedPosts = XX; // number of approved posts to add group to user
                            $addGroup = X; // use the Id of the group you want to add
                            $adminGroup = X;
                            $modGroup = X;
                            $contGroup = X;
                            $donGroup = X;
                
                            $user = $event->post->user;
                            // Skip if user is already part of group
                            if ($user->groups->find($addGroup) !== null) return;
                            if ($user->groups->find($adminGroup) !== null) return;
                            if ($user->groups->find($modGroup) !== null) return;
                            if ($user->groups->find($contGroup) !== null) return;
                            if ($user->groups->find($donGroup) !== null) return;
                
                            if ($user->posts()
                                      ->where('is_approved', 1)
                                      ->whereNull('hidden_at')
                                      ->count() >= $needsApprovedPosts) {
                                $user->groups()->attach($addGroup);
                            }
                        }),
                    
                (new Extend\Event)
                        // Add a user to a specific group after number of approved posts, requires flarum/approval.
                        ->listen(\Flarum\Post\Event\Posted::class, function (\Flarum\Post\Event\Posted $event) {
                            $needsApprovedPosts = XXX; // number of approved posts to add group to user
                            $addGroup = X;
                            $removeGroup = X; // use the Id of the group you want to add
                            $adminGroup = X;
                            $modGroup = X;
                            $contGroup = X;
                            $donGroup = X;
                
                            $user = $event->post->user;
                            // Skip if user is already part of group
                            if ($user->groups->find($addGroup) !== null) return;
                            if ($user->groups->find($adminGroup) !== null) return;
                            if ($user->groups->find($modGroup) !== null) return;
                            if ($user->groups->find($contGroup) !== null) return;
                            if ($user->groups->find($donGroup) !== null) return;
                
                            if ($user->posts()
                                      ->where('is_approved', 1)
                                      ->whereNull('hidden_at')
                                      ->count() >= $needsApprovedPosts) {
                                $user->groups()->detach($removeGroup);
                                $user->groups()->attach($addGroup);
                            }
                        }),

                  LiamKenyon i have not tested this, so I don't know if it works 😊

                      (new Extend\Event)
                          // Add a user to a specific group after number of approved posts, requires flarum/approval.
                          ->listen(\Flarum\Post\Event\Posted::class, function (\Flarum\Post\Event\Posted $event) {
                              // map post requirement => group id
                              $thresholds = [
                                  30  => 5,
                                  150 => 7
                              ];
                  
                              $groups = $event->post->user->groups ?? \Illuminate\Support\Collection::make();
                  
                              // Filter admin and moderator
                              if ($groups->whereIn('id', [
                                  \Flarum\Group\Group::ADMINISTRATOR_ID,
                                  \Flarum\Group\Group::MODERATOR_ID,
                                  // Add more Group Ids here to exclude them from the check
                              ])->isNotEmpty()) {
                                  return;
                              }
                  
                              // Get user post count
                              $postCount = $event->post->user->comment_count ?? 0;
                  
                              // we need to loop backwards to see if they match the highest threshold, we apply
                              // that group first
                  
                              foreach (array_reverse($thresholds) as $requiredCount => $groupId) {
                                  // If the user meets the requirement
                                  if ($postCount >= $requiredCount) {
                                      // get the removable group ids
                                      $removableGroupIds = \Illuminate\Support\Arr::except(array_values($thresholds), $groupId);
                  
                                      $event->post->user->getConnection()->transaction(function () use ($event, $groupId, $removableGroupIds) {
                                          // add the group
                                          $event->post->user->groups()->attach($groupId);
                                          // remove the other groups
                                          $event->post->user->groups()->detach($removableGroupIds);
                                      });
                                  }
                  
                              }
                          }),

                    luceos I did try this and it broke the forum 😆

                    I'm assuming I edit \Flarum\Group\Group::ADMINISTRATOR_ID to the number or is this stay the name of the group?

                    I'm current using the above and testing atm.

                        (new Extend\Event)
                            // Add a user to a specific group after number of approved posts, requires flarum/approval.
                            ->listen(\Flarum\Post\Event\Posted::class, function (\Flarum\Post\Event\Posted $event) {
                                $needsApprovedPosts1 = XX; // number of approved posts to add group1 to user
                                $needsApprovedPosts2 = XXX; // number of approved posts to add group2 to user
                                
                                $addGroup1 = X; // use the Id of the group1 you want to add
                                $addGroup2 = X; // use the Id of the group2 you want to add
                                
                                $adminGroup = X;
                                $modGroup = X;
                                $contGroup = X;
                                $donGroup = X;
                    
                                $user = $event->post->user;
                                // Skip if user is already part of group
                                if ($user->groups->find($addGroup1) !== null) return;
                                if ($user->groups->find($addGroup2) !== null) return;
                                
                                if ($user->groups->find($adminGroup) !== null) return;
                                if ($user->groups->find($modGroup) !== null) return;
                                if ($user->groups->find($contGroup) !== null) return;
                                if ($user->groups->find($donGroup) !== null) return;
                                
                                $postCount = $user->posts()
                                                  ->where('is_approved', 1)
                                                  ->whereNull('hidden_at')
                                                  ->count();
                                 
                                if ($postCount >= $needsApprovedPosts1 && $postCount < $needsApprovedPosts2) {
                                    $user->groups()->attach($addGroup1);
                                } elseif ($postCount >= $needsApprovedPosts2) {
                                    $user->groups()->detach($addGroup1);
                                    $user->groups()->attach($addGroup2);
                                }
                            }),
                    22 days later

                    Current "updated code im using"

                    <?php
                    
                    /*
                     * This file is part of Flarum.
                     *
                     * For detailed copyright and license information, please view the
                     * LICENSE file that was distributed with this source code.
                     */
                    
                    use Flarum\Extend;
                    use Flarum\User\User;
                    use Flarum\Post\Event\Posted;
                    
                    return [
                    
                        (new Extend\Event)
                        ->listen(Posted::class, function (Posted $event) {
                            $needsApprovedPosts1 = XX; // Number of approved posts to add user to group1
                            $needsApprovedPosts2 = XXX; // Number of approved posts to add user to group2
                    
                            $groupId1 = X; // Group ID to add user for group1
                            $groupId2 = X; // Group ID to add user for group2
                    
                            $excludedGroups = [X, X X, X]; // Excluded group IDs
                    
                            $user = $event->post->user;
                            $userGroups = $user->groups()->pluck('id')->toArray();
                    
                            // Skip if user is already part of group or belongs to excluded groups
                            if (in_array($groupId1, $userGroups) || in_array($groupId2, $userGroups) || array_intersect($excludedGroups, $userGroups)) {
                                return;
                            }
                    
                            $postCount = $user->posts()
                                ->where('is_approved', 1)
                                ->whereNull('hidden_at')
                                ->count();
                    
                            if ($postCount >= $needsApprovedPosts1 && $postCount < $needsApprovedPosts2) {
                                $user->groups()->attach($groupId1);
                            } elseif ($postCount >= $needsApprovedPosts2) {
                                $user->groups()->detach($groupId1);
                                $user->groups()->attach($groupId2);
                            }
                        })
                    ];