Home › Forums › ACF PRO › (Filter) update_value for Group field › Reply To: (Filter) update_value for Group field
I might be a bit off with this, but I ran into what I think is a similar issue. I look for an empty field (video thumbnail in my case), and if it’s empty, I look at another field (a vimeo video ID) and I do a little fetch of some json to grab the thumbnail URL and then save it. This has worked great for simple ACF fields.
I recently wanted to use this in a Group, and noticed the field wasn’t updating.
I had a few problems:
– I was using ‘acf/update_value/name=thumbnail_url’ which will match any field (including subfields it turns out) with that name. Cool for most of my other ACFs where I can just have one filter in functions.php to cover a bunch of different ACF fields where I need this. Not cool for this subfield, as the video ID was also in a subfield, so my function was not finding a video ID.
– I had no idea how to match a subfield in the “acf/update_value/name=foobar” format (still don’t)
– I had no idea how to fetch the subfield with the video ID
This was the original function/filter:
// Fetch vimeo thumbnail on save - takes video ID as input
function rf_acf_update_vimeo_thumb( $value, $post_id, $field ) {
// only set a new value if the field is empty
if ( $value == '') {
// get video here
$vimeo_video_id = get_field('vimeo_video_id');
// fetch thumb url(s)
$vimeo_meta = file_get_contents("https://vimeo.com/api/v2/video/$vimeo_video_id.json");
$vimeo_meta = json_decode($vimeo_meta);
$vimeo_thumb = $vimeo_meta[0]->thumbnail_large;
// set value
$value = $vimeo_thumb;
return $value;
} else {
return $value;
}
}
add_filter('acf/update_value/name=thumbnail_url', 'rf_acf_update_vimeo_thumb', 10, 3);
So to “fix” this:
– I opted to give my thumbnail field a new name since I don’t know how to match on a subfield only, so that’s now “wfh_thumbnail_url”.
– I randomly stumbled on another post in these forums that stated a subfield can be fetched by combining the group’s field name and the subfield name, so my “get_field()” for the video ID which is in the group “page_top” and is named “vimeo_video_id” is fetched with “get_field(‘page_top_vimeo_video_id’);”
With those two changes, things seem to work well.
// same, but sub-field
// Fetch vimeo thumbnail on save - takes video ID as input
function rf_acf_update_vimeo_thumb_alt( $value, $post_id, $field ) {
// only set a new value if the field is empty
if ( $value == '') {
// get video here
$vimeo_video_id = get_field('page_top_vimeo_video_id');
// fetch thumb url(s)
$vimeo_meta = file_get_contents("https://vimeo.com/api/v2/video/$vimeo_video_id.json");
$vimeo_meta = json_decode($vimeo_meta);
$vimeo_thumb = $vimeo_meta[0]->thumbnail_large;
// set value
$value = $vimeo_thumb;
return $value;
} else {
return $value;
}
}
add_filter('acf/update_value/name=wfh_thumbnail_url', 'rf_acf_update_vimeo_thumb_alt', 10, 3);
I do wish I could just match with ‘acf/update_value/name=page_top_wfh_thumbnail_url’…
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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.