Home › Forums › Add-ons › Repeater Field › Initialize repeater field or programmatically find field_key › Reply To: Initialize repeater field or programmatically find field_key
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'];
}
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.