Support

Account

Home Forums General Issues Dynamically populate between 2 repeater fields

Solved

Dynamically populate between 2 repeater fields

  • Need help

    I’m trying to dynamically populate a select field’s choices, using example 2 on https://www.advancedcustomfields.com/resources/dynamically-populate-a-select-fields-choices/

    I tried several scenario’s, but all failed.
    It’s seemed simple, but I’ve been struggling for some time … so I’m reaching out.

    I have 2 ACF groups

    1. ACFG01
    1a. ACFG01-F01 (Repeater field)
    1.1a ACFG-F01-SOURCE (Text Field)

    2. ACFG02
    2a. ACFG02-F01 (Repeater field)
    2.1a ACFG-F01-TARGET (Select Field)

    I would like 1) ACFG-F01-SOURCE to occupy ACFG-F01-TARGET and 2) keep ACFG-F01-TARGET up-to-date whenever ACFG-F01-SOURCE is updated.

    Any help or pointers are very welcome. Thanks in advance!

  • function acf_load_field_choices( $field ) {
        
        // reset choices
        $field['choices'] = array();
    
        // if has rows
        if( have_rows('ACFG01-F01', 'option') ) {
            
            // while has rows
            while( have_rows('ACFG01-F01', 'option') ) {
                
                // instantiate row
                the_row();
                
                
                // vars
                $value = get_sub_field('ACFG-F01-SOURCE');
                $label = get_sub_field('ACFG-F01-SOURCE');
    
                
                // append to choices
                $field['choices'][ $value ] = $label;
                
            }
            
        }
    
        // return the field
        return $field;
        
    }
    
    add_filter('acf/load_field/name=ACFG-F01-TARGET', 'acf_load_field_choices');

    Anyone?

  • As long as the field names are correct and the options page has values in the repeater, there is nothing in your code that looks incorrect.

    Is the repeater nested in another field (group, repeater, flex field, etc)?

    When you create the options page did you alter the default post_id to save values to?

  • Thanks for your reply. I figures it out. I concentrated solely on the load_field code and not on the Options Page, which is mandatory.

    For those struggling, add this to your functions.php

    if( function_exists('acf_add_options_page') ) {
    	
    	acf_add_options_page();
    	
    }

    and add your source data there.

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

The topic ‘Dynamically populate between 2 repeater fields’ is closed to new replies.