Support

Account

Home Forums Add-ons Repeater Field Dynamic Select from Repeater Siblings

Solved

Dynamic Select from Repeater Siblings

  • I have a repeater field with a select that I’m attempting to populate using the load_field filter. This works fine when grabbing values from another repeater, but when attempting to pull from sibling values, it’s causing an infinite loop (presumably because each time get_sub_field is called, the load_field filter is being fired again).

    My question boils down to this: is it possible to populate a select with its sibling values? Is it possible to load the values from a repeater without triggering the load_field filter?

  • I’m also having issues with an infinite loop while using get_field inside of a load_field filter. So annoying!

  • I have no idea if this will help in your situation, but I solved mine by using $wpdb->get_results. In my particular situation, I had a repeater of “Questions”. Each question had a text field “Question”, and a dropdown which is supposed to display the other items in the repeater.

    This code pulls the “question” field for each “questions” on the current post, and since it’s done directly through the $wpdb query it doesn’t trigger any of the ACF actions or filters, avoiding the infinite loop.

    
    function loadQuestions($field) {
        global $wpdb;
        $field['choices'] = array( 0 => 'None' );
            
        $choices = $wpdb->get_results($wpdb->prepare(
            "SELECT meta_value FROM wp_postmeta WHERE post_id = %d AND meta_key RLIKE %s",
            get_the_id(),
            sprintf('^%s_[0-9]*_%s$', 'questions', 'question')
        ), ARRAY_A);
    
        for ($i = 0; $i < count($choices); $i++) {
            $field['choices'][] = $choices[$i]['meta_value'];
        }
    
        return $field;
    }
    

    This isn’t a perfect solution — my number one issue with it is that it only pulls values that have already been saved in the database, so in my situation, I have to save all the questions before I can set any of the dropdowns. I’m 90% sure this could be solved with a little javascript, but I haven’t had the time to investigate further, and it’s not important enough on my end to warrant the time.

  • So basically you avoided using get_field() inside of your hook. 🙂

    I took another approach and removed the hook once it had been executed.

    remove_filter( 'acf/load_field/name=mb_recipe', array( $this, 'load_recipe_options' ) );

    Seems to have solved the trick for now. Still, which they could add a param to the hook arguments that would allow me to filter out what called the hook. 🙂

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

The topic ‘Dynamic Select from Repeater Siblings’ is closed to new replies.