One-Time Process: Remove Specific Follower from All Business Profile

Get Started

This document provides a one-time process to bulk remove a user from all business profile as follower

Code Snippet

function run_once_remove_user_from_business_groups() {
    // Ensure the function runs only once by using an option flag
    if (get_option('remove_user_from_business_groups_completed')) {
        return; // Stop execution if already run
    }

    // Specify the user ID to be removed (Replace 123 with the actual user ID)
    $user_id = 123;

    // Get all business profiles (replace 'business_profile' with your post type if different)
    $args = array(
        'post_type'      => 'business',
        'posts_per_page' => -1, // Get all posts
        'post_status'    => 'publish',
    );

    $business_profiles = get_posts($args);

    if (!$business_profiles) {
        echo 'No business profiles found.';
        return;
    }

    foreach ($business_profiles as $profile) {
        // Retrieve the group ID associated with the business profile
        $group_id = get_post_meta($profile->ID, 'bp-business-group', true);

        if ($group_id) {
            // Use BuddyPress function to remove the user from the group
            groups_remove_member($user_id, $group_id);
            echo "User {$user_id} removed from group {$group_id} (Profile: {$profile->post_title})<br>";
        } else {
            echo "No group ID found for profile: {$profile->post_title}<br>";
        }
    }

    // Mark the process as completed to prevent future executions
    update_option('remove_user_from_business_groups_completed', true);
}

// Hook the function to run during 'init'
add_action('init', 'run_once_remove_user_from_business_groups');

Instructions

  1. Add the code snippet to your theme’s functions.php file or a custom plugin.
  2. Replace 123 with the actual user ID you want to remove from the business groups.
  3. This function will run once and use the update_option() function to prevent future executions.
  4. After running, you can confirm the user is removed from the groups. You can then safely remove the code from your theme or plugin to keep things clean.
  5. This ensures the process runs smoothly without interfering with other site operations.

Notes

  • Ensure to test this on a staging site before running it on the live site.
  • Create a backup before making any changes, especially on production sites.
  • If you encounter any issues, check the browser console or server logs for errors.
Last updated: April 7, 2025