Support

Account

Home Forums ACF PRO (Filter) update_value for Group field

Solving

(Filter) update_value for Group field

  • Hi i have a group field
    with
    seo
    – title
    – description field

    my goal is when i not fill the title or description field in seo i want to fill it from the post title and excerpt

    add_filter('acf/update_value/name=seo', 'renka_fill_empty_seo_fields', 10, 3);
    
     function renka_fill_empty_seo_fields($value, $post_id, $field)
     {
         // $value is still empty
         var_dump($field);
     }
    

    but i have no idea how i can set the value for the group i know how i can do it when i use 2 filters and use the field names direct

  • I’m running into the same issue. There’s no clear indication of this behaviour in the documentation or on these forums / StackOverflow.

  • A group field operates on the same principle as a repeater field. It is a repeater field that always has 1 row. I have not tried it but I would assume that update_sub_field() is what you’re looking for https://www.advancedcustomfields.com/resources/update_sub_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’…

Viewing 4 posts - 1 through 4 (of 4 total)

The topic ‘(Filter) update_value for Group field’ is closed to new replies.