Home › Forums › Add-ons › Repeater Field › How to add row to ACF repeater field in Term
I have a taxonomy with an ACF repeater field. I am trying to add rows in a callback for a custom REST API endpoint and having no luck. The scheme for the field is:
`
{
“key”: “field_5faa2bc09fe7b”,
“label”: “team”,
“name”: “team”,
“type”: “repeater”,
“instructions”: “”,
“required”: 0,
“conditional_logic”: 0,
“wrapper”: {
“width”: “”,
“class”: “”,
“id”: “”
},
“collapsed”: “”,
“min”: 0,
“max”: 10,
“layout”: “table”,
“button_label”: “”,
“sub_fields”: [
{
“key”: “field_5faa2bce9fe7c”,
“label”: “Role”,
“name”: “role”,
“type”: “post_object”,
“instructions”: “”,
“required”: 0,
“conditional_logic”: 0,
“wrapper”: {
“width”: “”,
“class”: “”,
“id”: “”
},
“post_type”: [
“role”
],
“taxonomy”: “”,
“allow_null”: 0,
“multiple”: 0,
“return_format”: “id”,
“ui”: 1
},
{
“key”: “field_5faa2c0b9fe7d”,
“label”: “User”,
“name”: “user”,
“type”: “user”,
“instructions”: “”,
“required”: 0,
“conditional_logic”: 0,
“wrapper”: {
“width”: “”,
“class”: “”,
“id”: “”
},
“role”: “”,
“allow_null”: 0,
“multiple”: 0,
“return_format”: “id”
}
]
}
`
I use the following to add a row to the field – and having no luck:
`
function wp_api_assign_internal_writer() {
register_rest_route( ‘mynamespace/v1’, ‘assign_internal_writer/’, array(
‘methods’ => ‘POST’,
‘callback’ => ‘assign_internal_writer_callback’,
));
}
function assign_internal_writer_callback( $request ) {
$parameters = $request->get_params();
$task = $parameters[‘task’];
$user = $parameters[‘user’];
$project_set = $parameters[‘project_set’];
if($task != ” && $user != ” && $project_set != ” ){
$row = array(
‘field_5faa2bce9fe7c’ => ‘268’,
‘field_5faa2c0b9fe7d’ => $user
);
add_row(‘team’, $row, $project_set);
return get_term_meta($project_set);
}else{
return ‘Please supply the correct request parameters, Task ID and User ID’;
}
}
`
Any idea what I am doing wrong? Does add row work only for Post and not Term….?
Some suggestions
insure that the values are integers since they represent IDs of one kind of another.
Use the field key here instead of the field name
add_row('team', $row, $project_set);
Thank you, I did end up using integers in my ultimate solution. However, I couldn’t get add_row to work, my ultimate solution was the following:
function assign_internal_writer_callback($request)
{
$parameters = $request->get_params();
$outline_task = $parameters['outline_task']; //ID for Task associated with Stage Two
$write_task = $parameters['write_task']; //ID for Task associated with Stage Five
$user = $parameters['user']; //User ID
$project_set = $parameters['project_set']; //Project Set ID
if ($outline_task != '' && $write_task != '' && $user != '' && $project_set != '') {
update_post_meta($outline_task, 'user', $user);
update_post_meta($write_task, 'user', $user);
$team_size = get_term_meta($project_set, 'project_set_campaign_team');
$new_team_size = strval(intval($team_size[0] + 1));
//Need to follow this convention for field keys in order for them to display in UI
$user_field_key = '_project_set_campaign_team_' . $team_size[0] . '_user';
$role_field_key = '_project_set_campaign_team_' . $team_size[0] . '_role';
$user_key = 'project_set_campaign_team_' . $team_size[0] . '_user';
$role_key = 'project_set_campaign_team_' . $team_size[0] . '_role';
update_term_meta($project_set, 'project_set_campaign_team', $new_team_size); //This meta field holds the number of rows in the repeater, needs to be updated +1 to display in UI
update_term_meta($project_set, $role_key, 268);
update_term_meta($project_set, $role_field_key, 'field_5faa2bce9fe7c');
update_term_meta($project_set, $user_key, intval($user));
update_term_meta($project_set, $user_field_key, 'field_5faa2c0b9fe7d');
return get_term_meta($project_set);
} else {
return 'Please supply the correct request parameters: Outline Task ID, Write Task ID, Project Set ID and User ID';
}
}
I’ve had several issues where I tried to use ACF methods like update_field and add_row to solve a problem, and they weren’t working. I was able to update with native WP methods like update_post_meta and update_term_meta. Is there any reason I’m unaware of not to do this?
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.