Home › Forums › Add-ons › Repeater Field › update_field() with repeater fields issue
Hi,
I’m having a problem updating/appending rows in repeater fields. I’m trying to migrate all data from one table into user meta and keep getting the following error.
Catchable fatal error: Object of class WP_Error could not be converted to string in C:\wamp\www\baca\wp-content\plugins\advanced-custom-fields\core\fields\_functions.php
I’m not sure what I’m doing wrong as I’m following the resources at http://www.advancedcustomfields.com/resources/functions/update_field/ but not working. Below is the code I’m using. Can someone see whats wrong?
function add_members() {
global $wpdb;
$results = $wpdb->get_results( "SELECT * FROM members" );
foreach($results as $row) {
$new_user = array(
'user_login' => $row->CompanyName,
'user_pass' => wp_generate_password ( 12, false ),
'user_url' => $row->Website,
'user_email' => $row->Email,
'wp_capabilities' => 'Subscriber'
);
$pid = wp_insert_user($new_user);
if($pid) :
$field_key = "field_52f4fedbcdb79";
$post_id = $pid;
$value = get_field($field_key, $post_id);
$value[] = array(
"more_contacts_name" => $row->ContactName,
"more_contacts_email" => $row->ContactEmail,
"acf_fc_layout" => "row_1"
);
$value[] = array(
"more_contacts_name" => $row->ContactName2,
"more_contacts_email" => $row->ContactEmail2,
"acf_fc_layout" => "row_2"
);
update_field( $field_key, $value, $post_id );
endif;
}
}
add_action( 'edit_user_profile', 'add_members' );
Hi @chrisg
$post_id = $pid;
should be $post_id = "user_{$pid}";
Please also debug your code before using the update_field function to make sure all the data is correct.
it is most likely that $pid
is not what you think it is.
Thanks
E
Brilliant, thanks Elliot. Thats solved it. I didn’t realise you had to add a prefix to the newly created user id. Seems pretty obvious now as the id could be for anything.
Thanks
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!
Are you building WordPress sites with ACF and @BeaverBuilder, and wanted to use your ACF Blocks in both the block editor and Beaver Builder?
— Advanced Custom Fields (@wp_acf) May 10, 2023
The BB team recently added support for using ACF Blocks in Beaver Builder. Check it out 👇https://t.co/UalEIa5aQi
© 2023 Advanced Custom Fields.
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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.