Home Forums ACF PRO Get value of another field in same group as current field


Get value of another field in same group as current field

  • Hi!

    I have a text field that I am trying to modify the value of, based on the value of another field within the same ACF field group (although it’s a clone, but with no prefixing).

    The UI perhaps shows this best:

    I am planning on replacing [highlight] with a <span style=”color: $color_of_other_field”> – where $color_of_other_field is “Highlight Colour” (or ‘highlight_color’)

    The text styling fields are all from a clone, so I’m not sure if that affects what the ‘group’ context/boundary is

    So far I am using

      add_filter( 'acf/format_value/type=text', 'add_highlight_html' );
      function add_highlight_html( $value, $post_id, $field )
        $value = str_replace( '[highlight]', '<span class="highlight">', $value );
        $value = str_replace( '[/highlight]', '</span>', $value );
        return $value;

    This works.

    However, to get the value of the ‘highlight_color’ field in the same group is what I am struggling with.

    I figured if I could get the ‘parent’ group key/ID, I might be able to query the fields within it, find the ‘highlight_color’ field, and then get the value.

    If I var_dump($field), I am given the parent ID (an integer, not key) as shown here (cut out unecessary attributes from array to save space):

    array (size=24)
      'ID' => int 2126
      'key' => string 'field_580900ab69edb' (length=19)
      'name' => string 'hero_columns_0_elements_2_text' (length=30)
      'parent' => int 2123
      'parent_layout' => string '580900ab69eda' (length=13)

    So what I’m trying to figure out is how to query the parent (with the ID of 2123) and then query the ‘highlight_color’ field in question to retrieve the value.

    If someone reading this is able to give me some pointers, I’d greatly appreciate it. Thanks.

  • Hi @db9429

    Please keep in mind that how you get the cloned value depends on your clone field settings. If you have the standard settings (Prefix Field Names set to “No”), then you can just get the highlight_color field as usual. Don’t forget to pass the current post ID like this:

    add_filter( 'acf/format_value/type=text', 'add_highlight_html' );
    function add_highlight_html( $value, $post_id, $field )
        $color_of_other_field = get_field('highlight_color', $post_id);
        $value = str_replace( '[highlight]', '<span class="highlight" style="color: '. $color_of_other_field .'">', $value );
        $value = str_replace( '[/highlight]', '</span>', $value );
        return $value;

    This page should give you more idea about it:

    I hope this helps 🙂

  • Hi James

    Thank you for the reply!

    I think I’m “OK” with the clone field side of things, actually. I was mentioning for context, but I think you’re completely right – it’s irrelevant in this context.

    One crucial thing I somehow didn’t mention is that the value I’m trying to get is inside a flexible content layout, nested inside a repeater.

    The fields look like:

    - Repeater (columns) - attached to a CPT single
    	- Flex layout blocks (inside columns)
    		- Text field (inside Flex layout)
    		- Highlight field (inside Flex layout)

    For this reason, get_field('highlight_color', $post_id) can’t retrieve the correct value.

    Through the acf/format_value/type=text filter, I am able to get the info as per my initial post, including the parent ID and the parent_layout key.

    So in this case, the current field ID is 2126 and it’s parent is 2123.

    What I’m therefore trying to do (in pseudo code) is:

    Get the value of a field who's parent is 2123 and name is highlight_color

    Thanks again

  • Hi @db9429

    Could you please share the JSON export file of your field group so I can test it out on my installation?

    Thanks 🙂

  • Hi James

    That’s very kind of you! 🙂

    It’s here:

    There’s more field groups than just the ones in question; I’m just to trying and avoid missing fields from clones.

    – The button field is in ‘Base: Button’
    – The flexible layouts are in ‘Hero: Elements’ and is assigned to a ‘Hero’ CPT (but you can reassign this to a page)
    – Inside the flex layouts, you’ll find the buttons layout, which is a repeater
    – I also included ‘Theme Options: Promo Bar’ – this has a button field too, but isn’t inside a repeater/flex

    Any questions, let me know. What I’m trying to do might well not possible – I’m prepared for that eventuality 🙂

    Appreciate it,

  • Hi @db9429

    I am sorry for the delayed response.

    I’ve just checked your export file but failed to find the “highlight color” and the text fields you were talking about. Is this a completely different setup? Could you please explain what do you want to achieve with this setup?

    Thanks 🙂

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

The topic ‘Get value of another field in same group as current field’ is closed to new replies.