Support

Account

Home Forums ACF PRO How to get repeater data to be used as a select?

Solved

How to get repeater data to be used as a select?

  • I’m trying to find a way to get the title in a repeater to be used as a select. I tried the example on one of the documentation but the select is empty:

    	function acf_load_color_field_choices( $field ) {
        
    		// reset choices
    		$field['choices'] = array();
    		
    		
    		// get the textarea value from options page without any formatting
    		$choices = get_field('playlist_title', false);
    	
    		
    		// explode the value so that each line is a new array piece
    		$choices = explode("\n", $choices);
    	
    		
    		// remove any unwanted white space
    		$choices = array_map('trim', $choices);
    	
    		
    		// loop through array and add to field 'choices'
    		if( is_array($choices) ) {
    			
    			foreach( $choices as $choice ) {
    				
    				$field['choices'][ $choice ] = $choice;
    				
    			}
    			
    		}
    		
    	
    		// return the field
    		return $field;
    		
    	}
    	
    	add_filter('acf/load_field/name=color', 'acf_load_color_field_choices');

    I was able to list all text fields with playlist_title using this code:

    <?php
    				// check for rows (parent repeater)
    				if( have_rows('media_playlist') ): ?>
    					<?php 
    					// loop through rows (parent repeater)
    					while( have_rows('media_playlist') ): the_row(); ?>
    							<?php the_sub_field('playlist_title'); ?>
    					<?php endwhile; // while( has_sub_field('to-do_lists') ): ?>
    				<?php endif; // if( get_field('to-do_lists') ): ?>

    Thanks for the help.

  • So I tried using this solution from https://support.advancedcustomfields.com/forums/topic/return-all-values-from-a-repeater-row-by-using-a-single-dynamic-select/ and it deletes the select field and adds two random blank fields

    function acf_load_contact_field_choices( $field ) {
        
    		// reset choices
    		$field['choices'] = array();
    	
    		// if has rows
    		if( have_rows('media_playlist') ) {
    			// while has rows
    			while( have_rows('media_playlist') ) {
    				// instantiate row
    				the_row();
    				
    				// vars
    				$contactName = the_sub_field('playlist_title');
    	
    				// append to choices
    				$field['choices'][ $contactName ] = $contactName;
    			}
    		}
    	
    		// return the field
    		return $field;
    		
    	}
    	add_filter('acf/load_field/name=color', 'acf_load_color_field_choices');
  • The filters you are trying to use modify the choices of fields in the admin. They will not create a select field for you.

    
    if (have_rows('your-repeater')) {
      ?>
        <select>
          <?php 
            while (have_rows('your-repeater')) {
              the_row();
              ?><option value="<?php the_sub_field('your-sub-field'); ?>"><?php the_sub_field('your-sub-field'); ?></option><?php 
            }
          ?>
        </select>
      <?php 
    }
    
Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘How to get repeater data to be used as a select?’ is closed to new replies.