Home › Forums › Backend Issues (wp-admin) › update_field() with field type User
Hi,
I need help with a current project, which deals with having to add new users to the CPT using the field type Users when new user purchases a product.
I was hoping I could do it through functions.php.
Get the value of the field without formatting and supply the post ID of the post that you want to update. Get Field: https://www.advancedcustomfields.com/resources/get_field/; Update Field: https://www.advancedcustomfields.com/resources/update_field/
// $post_id is the ID of the post you want to update
// 3rd arg == false tells ACF not to format the value
// $users will == an array or user IDs
$users = get_field('user_field_name', $post_id, false);
// add the users ID to the array
$users[] = $new_user_id_to_add;
// update the field
// $selector == the field key of the field to update
update_field($selector, $users, $post_id);
This is what I currently have.
elseif ($product_id == 1126) {
$e_post = get_page_by_title( $title );
$epost_id = $e_post->ID;
$member = wp_get_current_user();
$newmember = $member->ID;
update_field( ‘comp_members’, $newmember, $epost_id);
echo “New Member, $newmember , added to $title.”;
} // End of new member
Thoughts?
// ...................
} elseif ($product_id == 1126) {
$e_post = get_page_by_title( $title );
$epost_id = $e_post->ID;
$member = wp_get_current_user();
$newmember = $member->ID;
$members = get_field('comp_members', $epost_id, false);
if (!is_array($members)) {
$members = array();
}
$members[] = $newmember;
update_field( 'comp_members', $newmember, $epost_id);
echo "New Member, $newmember , added to $title.";
} // End of new member
Hi John,
I was actually able to fix this issue already. The error came from the line:
WRONG line : $e_post = get_page_by_title( $title );
CORRECT line : $e_post = get_page_by_title($title, OBJECT, ‘company’);
The topic ‘update_field() with field type User’ is closed to new replies.
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.