Running A Calculation On All Users Every Month
-
I want to schedule a calculation to run against every user on my site each month. I can setup some logic to limit the number of users this will run against e.g. if they’ve had any activity, but the crux of the matter is I have a user base of at least 25,000 users and need to calculate an average of 6 and (separately) 12 values of meta data on their account.
I’ve come up with something like this – which not surprisingly causes the site to crash:
function user_monthly_roi_calculation() { /* Get all users */ global $wpdb; $users = $wpdb->get_results("SELECT ID from $wpdb->users"); foreach ($users as $user) { // Calculate 6 month ROI for user $average_6 = array(); for ($i = 0; $i <= 5; $i++) { $user_meta = "month_profit_" . $i; $six_month[] = get_the_author_meta( $user_meta, $user->ID ); } $average_profit_6 = array_sum($average_6) / count($average_6); echo $average_profit_6; //update_user_meta( $user->ID, 'average_profit_6', $average_profit_6 ); // Calculate 12 month ROI for user $average_12 = array(); for ($i = 0; $i <= 11; $i++) { $user_meta = "month_profit_" . $i; $twelve_month[] = get_the_author_meta( $user_meta, $user->ID ); } $average_profit_12 = array_sum($average_12) / count($average_12); //update_user_meta( $user->ID, 'average_profit_12', $average_profit_12 ); // Rotate user ROI values for ($i = 0; $i <= 11; $i++) { $user_meta_src = "month_profit_" . $i; $user_meta_tar = "month_profit_" . ($i + 1); //update_user_meta( $user->ID, $user_meta_tar, $user_meta_src ); } // Reset current month to 0 //update_user_meta( $user->ID, "month_profit_0", 0 ); } } add_action('monthly_event','user_monthly_roi_calculation');
Any suggestions on how I can get this running in such a way that the site isn’t brought down each month ??
Viewing 4 replies - 1 through 4 (of 4 total)
Viewing 4 replies - 1 through 4 (of 4 total)
- The topic ‘Running A Calculation On All Users Every Month’ is closed to new replies.