Home › Forums › Add-ons › Repeater Field › Initialize repeater field or programmatically find field_key
Hi,
I’ve been searching the forums but I can’t seem to find the exact solution.
I have a routine where I want to add a new row to a repeater field on a user, whenever they submit a form or comment. I have cracked it using the code below for when the field has been initialised (ie. saved once in the CMS) but this isn’t practical to do as there are hundreds of users.
function acf_add_repeater_row($field_name, $post_id, $values) {
$field_key = get_field_object($field_name, $post_id );
// check whether the meta fields exist in the DB
if( $field_key['value'] ) {
// they do, let's add a row
array_push( $field_key['value'], $values );
update_field( $field_key['key'], $field_key['value'], $post_id );
}
else {
// they don't, let's create them
// ???
}
}
I would like to make this code portable, so I was wondering if there is a way to either initialise the field before saving to it, or to programmatically find the field key (rather than looking at the custom fields page)?
Thanks,
Adam
Hi @Adam Ainsworth
If the field has not been saved to the post, then it is not possible for ACF to find the correct field object or field_key.
You will need to use pass in the field_key to the $field_name parameter of your function. Please note that the get_field_object function will work correctly using the field_key.
By using the field key, the field object will always be found.
I hope this helps
Cheers
E
Hi @elliot – thanks very much for your response.
I have altered the function so that I can pass it the field key – is there really know way to find this?
I have one final problem, if you don’t mind? My code is below, it is not correctly inserting the data when no rows exist already (the second update_field call).
Thanks,
Adam
function acf_add_repeater_row($field_name, $post_id, $values, $field_key = '') {
$field_obj = get_field_object($field_name, $post_id );
// var_dump($field_obj['value']);
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
update_field( $field_key, $values, $post_id );
}
}
Hi @Adam Ainsworth
I can’t explain why your data is not being updated as expected, but please debug your code line by line and test each variable:
http://www.advancedcustomfields.com/resources/how-to/debug/
Thanks
E
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
I am also able to populate a repeater field, but only by hard coding the field key. Is there a way around this so that I can run this on any WordPress install without updating the field key by hand in code for each one?
Thank you,
Jasper
If it helps anyone, I put together this function for ACF 4 to get the correct field key for a given post and field name to match:
/**
* Get field key for field name. ACF 4
* Will return first matched acf field key for a given field name to match.
*
* This function will return the field_key of a certain field.
*
* @param $post_id int The post id to check.
* @param $field_match String The field name to match in the query in SQL format e.g. '%versions%'
* @return
*/
private function acf_get_field_key( $post_id, $field_match ) {
global $wpdb;
$posts = $wpdb->get_results( $wpdb->prepare( "SELECT ID FROM $wpdb->posts where post_type=%s", 'acf' ) );
$rows = $wpdb->get_results( $wpdb->prepare("SELECT meta_key,meta_value FROM $wpdb->postmeta WHERE post_id = %d AND meta_key LIKE %s AND meta_value LIKE %s", $posts[0]->ID, 'field_%', $field_match), ARRAY_A);
$field_data = unserialize( $rows[0]["meta_value"] );//not sure why it is in there twice...
return $field_data['key'];
}
The topic ‘Initialize repeater field or programmatically find field_key’ 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.