Home › Forums › Add-ons › Repeater Field › Initialize repeater field or programmatically find field_key › Reply To: Initialize repeater field or programmatically find field_key
Hi Elliott,
sorry for the delay in getting back to you – I’ve been working on other projects.
It turns out my code was nearly correct. What I needed to do when no value exist was have an array of arrays.
So, I have just added one line to my code, and it seems to be working now
function acf_add_repeater_row($field_name, $post_id, $values, $field_key = '') {
$field_obj = get_field_object($field_name, $post_id );
if( $field_key == ''){
$field_key = $field_obj['key'];
}
// check whether the meta fields exist in the DB
if( $field_obj['value'] ) {
// they do, let's add a row
array_push( $field_obj['value'], $values );
update_field( $field_key, $field_obj['value'], $post_id );
}
else if ($field_key) {
// they don't, let's create them
$values = array( $values );
update_field( $field_key, $values, $post_id );
}
}
The only thing that is left is finding the field key programmatically, rather than having to hard code it
Adam
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.