Support

Account

Home Forums Add-ons Repeater Field Dynamic Select from Repeater Siblings Reply To: Dynamic Select from Repeater Siblings

  • 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.