Support

Account

Home Forums ACF PRO How to Select Repeater Data from a Dropdown Reply To: How to Select Repeater Data from a Dropdown

  • Now that I look at what I have. I have a class that sores the repeater in an array so I don’t need to get it every time. Here is a very simple example:

    
    <?php 
      
      new your_class_name();
      
      class your_class_name {
        
        private $cta_choices = array();
        
        public function __construct() {
          add_filter('acf/prepare_field/name=your_field_name', array($this, 'choices'));
        }
        
        public function choices($field) {
          
          if (!empty($this->cta_choices)) {
            $field['choices'] = $this->cta_choices;
            return $field;
          }
          
          
          if (have_rows('cta_repeater', 'options')) {
            $choices = array();
            while (have_rows('cta_repeater', 'options')) {
              the_row();
              $class = get_sub_field('id_field_name'); // this is my unique ID field
              $text = get_sub_field('title_field_name');
              $choices[$class] = $text;
            } // end while have rows
            $field['choices'] = $choices;
          }
          
          $this->cta_choices = $field['choices'];
          
          return $field;
          
        }
        
      }