Howdy, Stranger!
It looks like you're new here. If you want to get involved, click one of these buttons!
Dear ACF users.
The support forum will be temporarily closed from Friday 24th May until Wednesday 29th May. Over this period, I will be taking some time away from work(ish) and focusing on producing better documentation!Please use the search forms below (and google) to find documentation and previous discussions. There are loads of useful articles which I'm sure will help you out!
I apologize for the inconvenience. Thanks, Elliot
update_field repeater
-
I am not entirely sure the update_field function for the repeater fields is working correctly. I am trying to use the below code but for users. It seems to add something in the db, but for a completely different user meta field and saves as an array rather than splitting it out into individual rows.
$field_key = "repeater_field";
$value = get_field($field_name);
$value[0] = array("sub_field_1" => "Foo", "sub_field_2" => "Bar");
$post_id = "event_123";
update_field( $field_key, $value, $post_id ); -
Hi interactivebytes,
You say that you are saving this to a user, if so, you have the wrong post id.
"event_123" should be "user_" . $user_id; -
Also, the get_field($field_name) needs to have this post_id to correctly load the value from a user
-
This is what I have. I have a repeater field called 'positions' with sub fields 'position_title' and 'industry'. There are other sub fields. (I assume I don't need to pass in all sub fields to just update two)
if (($_POST)&&($_POST["positions"])){
$field_key = "repeater_field";
$value = get_field('positions', 'user_'.$current_user->ID);
for ($i = 0; $i < $_POST["positions"]; $i++) {
$value[0] = array("position_title" => $_POST["positions_" . $i . "_position_title"], "industry" => $_POST["positions_" . $i . "_industry"]);
update_field( $field_key, $value, 'user_'.$_POST["userID"]);
}
} -
Any ideas? The things I am unsure about are...
$value[0] - Does the 0 represent the first record in the repeater?
array("sub_field_1" => - Does the sub_field_1 want to 'position_title' or 'field_504ed3eb281e2' ? -
Hi interactivebytes,
I think your main issue is that you are using different field names for both get_field and update_field.
Also, you code uses different vars for the same thing ( $current_user->ID vs $_POST["userID"]).
As for the $value[0], yes, that is the first row. If you want to add a row to the values instead of overriding the first row, you could use this:
For each row, the key for the sub field should be the sub_field_name.
If you need any extra help, please post what you get for
';
echo '';
print_r( $_POST);
echo '
die;
Hope that helps. -
Thanks, so $field_key = "repeater_field"; actually wants to be $field_key = "positions";
Is there a way to override the caching that gets applied to the get field functions? I am submitting the form and saving fields, but they don't return automatically. I tried putting in a ?saved=true param on the end so it thinks it's a new page, but nothing. -
Hi nteractivebytes,
Can you confirm that the data is being saved correctly? -
Yeh it is saving now, thanks. I am having a problem with the user field add on, where if you save via the backend post section it saves into the db as a user id, where as if you use the the update_field with the repeater as per above, it saves as an array with all sorts of user info.
-
Would I need to contact the developer of the add on? Or is this your thing? Thanks
-
Hi @interactivebytes
Perhaps you are saving the wrong value.
It sounds like normally, the user_field is expecting an ID, but you are maybe saving the entire user object?
Could this be the case? -
Using the update_field function for repeater fields, it adds all ok for the other values, but it even seems to rewrite the user id for all other entries with the repeater. So even though I may be passing in for row 3 it updates row 1 and 2 user id. So it rewrites values that were already there.
-
Hi @interactivebytes
Yes, when the repeater field saves, it saves all it's data. Asking it to compare against the database would require more PHP memory / time.
This is more efficient I believe.
Is there a problem or can this be marked as Solved? -
The problem is it rewrites all the user ids as a WP_User array. So if previously it has user_id = 2 before, but when you save form the front end using update_field, all user_ids are now written like
O:7:"WP_User":7:{s:4:"data";O:8:"stdClass":10:{s:2:"ID";s:1:"2";s:10:"user_login";s:8:"employee";s:9:"user_pass";s:34:"blah";s:13:"user_nicename";s:8:"employee";s:10:"user_email";s:27:"blah";s:8:"user_url";s:0:"";s:15:"user_registered";s:19:"2012-08-28 10:22:21";s:19:"user_activation_key";s:0:"";s:11:"user_status";s:1:"0";s:12:"display_name";s:8:"employee";}s:2:"ID";s:1:"2";s:4:"caps";a:1:{s:10:"getstarted";s:1:"1";}s:7:"cap_key";s:15:"wp_capabilities";s:5:"roles";a:1:{i:0;s:10:"getstarted";}s:7:"allcaps";a:3:{s:4:"read";s:4:"true";s:10:"edit_users";s:1:"1";s:10:"getstarted";s:1:"1";}s:6:"filter";s:0:"";} -
Hi @interactivebytes
Can you post the value of $value right before you use the update_field function?
Thanks -
This is the value of $value after I I use this:
$value = get_field('appointment', $_POST["jobID_save"]);
So it looks to return the user array from the get_field function, even though it starts as a single user id in the db for those fields. To keep on trucking with this project, I just keep updating the db field back to their ids.
Array
(
[0] => Array
(
[date] => 20121128
[time] =>
[mother] => WP_User Object
(
[data] => stdClass Object
(
[ID] => 2
[user_login] => employee
[user_pass] => *hidden so I can show you this*
[user_nicename] => employee
[user_email] => *hidden so I can show you this*
[user_url] =>
[user_registered] => 2012-08-28 10:22:21
[user_activation_key] =>
[user_status] => 0
[display_name] => employee
)
[ID] => 2
[caps] => Array
(
[getstarted] => 1
)
[cap_key] => wp_capabilities
[roles] => Array
(
[0] => getstarted
)
[allcaps] => Array
(
[read] => true
[edit_users] => 1
[getstarted] => 1
)
[filter] =>
)
[desired_time] => Afternoon
[status] =>
[shortlist] =>
)
[1] => Array
(
[date] => 20121129
[time] =>
[mother] => WP_User Object
(
[data] => stdClass Object
(
[ID] => 2
[user_login] => employee
[user_pass] => *hidden so I can show you this*
[user_nicename] => employee
[user_email] => *hidden so I can show you this*
[user_url] =>
[user_registered] => 2012-08-28 10:22:21
[user_activation_key] =>
[user_status] => 0
[display_name] => employee
)
[ID] => 2
[caps] => Array
(
[getstarted] => 1
)
[cap_key] => wp_capabilities
[roles] => Array
(
[0] => getstarted
)
[allcaps] => Array
(
[read] => true
[edit_users] => 1
[getstarted] => 1
)
[filter] =>
)
[desired_time] => Afternoon
[status] =>
[shortlist] =>
)
[2] => Array
(
[date] => 20121030
[time] =>
[mother] => WP_User Object
(
[data] => stdClass Object
(
[ID] => 2
[user_login] => employee
[user_pass] => *hidden so I can show you this*
[user_nicename] => employee
[user_email] => *hidden so I can show you this*
[user_url] =>
[user_registered] => 2012-08-28 10:22:21
[user_activation_key] =>
[user_status] => 0
[display_name] => employee
)
[ID] => 2
[caps] => Array
(
[getstarted] => 1
)
[cap_key] => wp_capabilities
[roles] => Array
(
[0] => getstarted
)
[allcaps] => Array
(
[read] => true
[edit_users] => 1
[getstarted] => 1
)
[filter] =>
)
[desired_time] => Afternoon
[status] =>
[shortlist] =>
)
[3] => Array
(
[desired_time] => Midday
[date] => 20121028
)
) -
Hi @interactivebytes
For the value you have posted above it is clear that the field uses the get_value_for_api function to modify the value from an ID to a user object.
You need to only save the ID, not the entire object.
Cheers
Elliot -
I'm not passing in an object, only the ID and in some cases, I am not even passing in the user_id for mother, only date changes, but the function updates all entries for mother to the WP_User object.
The above $value gets returned from the get_field function, so that function in the plug-in is converting the variable in the db to an object and passing it back to me to pass into update_field.
I understand it would return the object for get_field as the add-on allows you to return many array options. But I guess when we want to use it to save it back into the db, we need it clean. -
Hi @interactivebytes
Yes, you can loop through the value and override each user object to the user ID,
then the update_field should work as expected -
@admin, I'm having the same issue as @interactivebytes. When I use update_field on a repeater field, the user ID for the newly inserted row is a single number, but any other rows' user IDs are updated to objects.
Here is what I'm using:$bid = intval ( $_POST['board'] );
$uid = get_current_user_id();
$field_key= 'field_5077ae931b6f6';
$value = get_field ( $field_key, $bid );
$value[] = array ( 'board_members_user' => $uid, 'can_collaborate' => 'n' );
$success = update_field ( $field_key, $value, $bid );
So, update_field works great for the newly added row, but updates all other rows to user objects. Is this a bug in my code or yours? -
Hi @vossavant
When you use get_field, all the values will be formatted (user ID will be converted to user object, etc)
So when you add the new row (which is fine), you are saving all the other rows with the wrong data. All the other rows will have this converted object instead of the ID.
Perhaps I will add in an option into the get_field function to allow for no formatting...
Thanks for the idea
Elliot -
@admin thanks for the response. So for the time being would you recommend I either avoid using get_field (I guess use a custom query instead?), or, after adding the row, go back and update all the rows that have user objects in them, as @interactivebytes did? I'm casting my vote for adding an option to exclude formatting. ;)
-
Hi @vossavant
I've already added it to the to-do!
for now, I would just write a custom add_meta query. Check out your database to see how the data is saved.
You will need to add in the new rows for the sub field data and also update the total count of rows on the parent custom field value
Good luck! -
Hi @vossavant,
Not sure if this will help.$field_key = "appointment";
$value = get_field('appointment', $_POST["jobID_save"]);
$count = count($value);
if ($value){
for($i=0;$i<count($value);$i++) {
$motherID = $value[$i]['mother'];
$value[$i]["mother"] = $motherID->ID;
}
$value[$count] = array("desired_time" => $_POST["desired_time"], "date" => $_POST["date"], "appointment_type" => $_POST["appointment_type"]);
update_field($field_key, $value, $_POST["jobID_save"]);
update_post_meta($_POST["jobID_save"], "appointment_" . $count . "_mother", $_POST["candidateID"]);
}
So I got my post variables and my repeater field is 'appointment'. 'mother' is my user objects. So I get the current $value then loop through and replace the mother value in array with just the ID. -
hi @interactivebytes
By using the update_field function, you don't need to use the update_post_meta part of your code.
Also, as you are just adding a new item to the end of the repeater, you can change:
$value[$count] =
to
$value[] = -
@admin and @interactivebytes,
Here's the code to add a new row to a repeater field without affecting the other rows, just in case anyone else needs it. Thanks for your help!// get row count
$total_rows = get_post_meta ( $post_id, 'your_custom_field', true );
// $total_rows is the number of rows prior to adding a new row, so we can also use it as an index
add_post_meta( $post_id, 'your_custom_field_'.$total_rows.'_your_subfield', $your_meta_value, true );
add_post_meta( $post_id, '_your_custom_field_'.$total_rows.'_your_subfield', 'field_50831e250a1bc', true );
// update row count
$success = update_post_meta( $post_id, 'your_custom_field', $total_rows + 1 );