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'];
}
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
Hello @elliot
Thank you for the excellent products!
I have been looking at as many resources as I can find online, trying directly calling update_field() as well as using acf/load_value, but I cannot seem to set a repeater field on a newly created page (all in code). Do you have any code snippets wherein a page is created and then an acf repeater field is successfully populated and then read back?
Thank you
Jasper
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.