Home › Forums › Add-ons › Repeater Field › Get row index inside add_filter( 'acf/load_field' ) › Reply To: Get row index inside add_filter( 'acf/load_field' )
I had a similar issue where i needed to add the ACF fields meta_id. Below is my solution to the same problem
function build_meta_key($field){
// Loop over parents.
$return_name = $field["_name"];
if(empty($field["prefix"])){
return $return_name;
}
$is_row = preg_match("/row-(\d)+/", $field["prefix"], $matches);
if(!$is_row){
return $return_name;
}
//now figure out the row number from the field name
$return_name = $matches[1]."_".$return_name;
while ( $field['parent'] && $field = acf_get_field( $field['parent'] ) ) {
if(isset($field["_name"])){
$return_name = $field["_name"]."_".$return_name;
}
}
return $return_name;
}
function get_all_meta_info_by_key($post_id, $meta_key, $meta_type = "post"){
global $wpdb;
if ( ! $meta_type || ! $post_id || trim($meta_key) == "") {
return false;
}
$table = _get_meta_table( $meta_type );
if ( ! $table ) {
return false;
}
$meta = $wpdb->get_row( $wpdb->prepare("SELECT * FROM {$table} WHERE post_id = %d AND meta_key = %s", $post_id, $meta_key) );
if ( empty( $meta ) ) {
return false;
}
if ( isset( $meta->meta_value ) ) {
$meta->meta_value = maybe_unserialize( $meta->meta_value );
}
return $meta;
}
/**
* Add in the ACF fields meta id to the field itself. so we can build the download links
* @param $field
*
* @return mixed
*/
function prepare_field( $field ) {
global $post;
$acf_meta_key = $this->build_meta_key($field);
$meta_information = $this->get_all_meta_info_by_key($post->ID, $acf_meta_key);
if(!empty($meta_information->meta_id)){
$field["post_meta_id"] = $meta_information->meta_id;
}
return $field;
}
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.