Home › Forums › Front-end Issues › Update ACF field in Users table
Hi Everyone,
I have been racking my brain and running code via AI to try and get a resolution to what I am looking for, I have a customer that has an ACF field named assigned_rep registered in the User Table, I am trying to improve the process that is currently done when they reassign reps, currently we edit each account manually using a Gravity Forms process I have built and it works, but when they do a bulk reassingment, that is over 900 accounts to update via Gravity Forms, one user at a time.
I have created a process that works with Posts to update an ACF field with the current assigned rep, if something changes, i update the code snippet and then I can go thru all posts pretty quick with just clicking an link on a page and it updates the post to the new assigned rep, I can get all the posts done in less than than the 900 accounts (there are over 11k posts to update when they do this reassignment.
Below is the code that AI says will work, however when I click the update rep link I have, it never updates and debug log shows it should update to the right value.
Any input is appreciated (you won’t hurt my feelings if you say the code is garbage)
Thanks!
/**
* Updates the user meta field 'assigned_rep' based on the value in 'federal_id'.
*
* @param int $user_id The user ID to update
* @return bool True if updated, false otherwise
*/
function update_assigned_rep_based_on_federal_id($user_id) {
// Get the federal_id from user meta
$federal_id = get_user_meta($user_id, 'federal_id', true);
// If federal_id is empty, use default rep
if (empty($federal_id)) {
$assigned_rep = '[email protected]';
update_user_meta($user_id, 'assigned_rep', $assigned_rep);
return true;
}
// Check if federal_id matches special cases (example: PA-35-050, 35-050, etc.)
if (preg_match('/^(PA-)?35-0?(50|51)/', $federal_id)) {
$assigned_rep = '[email protected]';
update_user_meta($user_id, 'assigned_rep', $assigned_rep);
return true;
}
// Extract the main number from federal_id (assuming format PA-XX or just numbers)
$number = null;
if (preg_match('/^PA-(\d+)/', $federal_id, $matches)) {
$number = intval($matches[1]);
} elseif (preg_match('/^(\d+)/', $federal_id, $matches)) {
$number = intval($matches[1]);
} else {
// Use default if no number found
$assigned_rep = '[email protected]';
update_user_meta($user_id, 'assigned_rep', $assigned_rep);
return true;
}
// Determine the assigned rep based on the number
if ($number >= 1 && $number < 16) {
$assigned_rep = '[email protected]';
} elseif ($number >= 16 && $number < 26) {
$assigned_rep = '[email protected]';
} elseif ($number >= 26 && $number < 41) {
$assigned_rep = '[email protected]';
} elseif ($number >= 41 && $number < 47) {
$assigned_rep = '[email protected]';
} elseif ($number >= 47 && $number < 61) {
$assigned_rep = '[email protected]';
} else {
$assigned_rep = '[email protected]';
}
// Update the user meta
update_user_meta($user_id, 'assigned_rep', $assigned_rep);
return true;
}
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.