Support

Account

Home Forums Backend Issues (wp-admin) ACF repeater options used as choices in ACF field

Solving

ACF repeater options used as choices in ACF field

  • Hi all,

    I have a question about ACF options. Basically, I an options page with a repeater field (products). I then have a select field in the post edit screen with the products from the options page pulled in. What I would like to do is have the choices in the post edit screen update with the data entered in the options page. Is this possible at all?

    Thanks
    Lee

  • You need to create an acf/prepare_field filter and get the values from the options.

    This documentation covers the basics https://www.advancedcustomfields.com/resources/dynamically-populate-a-select-fields-choices/

    But you should use the prepare field filter https://www.advancedcustomfields.com/resources/acf-prepare_field/ rather than the load field filter. acf/prepare_field was introduced after the other document was written

  • Thanks John, I will look into the links provided and give it a shot, will report back.

  • Hi John, I initially used the ‘Dynamically populate a select field’s choices’ you mentioned above, I have updated the code I had, changing all instances of ‘load_field’ to ‘prepare_field’.

    Unfortunately, that didn’t work.

    Here is my code:

    function acf_prepare_product_field_choices( $field ) {
        
        // reset choices
        $sub_field['products'] = array();
    
        // if has rows
        if( have_rows('product', 'option') ) {
            
            // while has rows
            while( have_rows('product', 'option') ) {
                
                // instantiate row
                the_row();
                
                
                // vars
                $value = get_sub_field('application_rate');
    			$label = get_sub_field('title');
    			$ar = get_sub_field('application_rate');
    
                
                // append to choices
                $sub_field['products'][ $value ] = $label;
                
            }
            
        }
        // return the field
        return $field;
    }
    
    add_filter('acf/prepare_field/name=products', 'acf_prepare_product_field_choices');

    To give further insight into my setup. I have an ACF Options group (Products) setup with repeater fields, looks like this:

    https://www.dropbox.com/s/yxas4b6lj2vhtgw/Screenshot%202021-02-19%20at%2018.56.57.png?dl=0

    I then have a flexible field setup where the admin can build out products to show on a plan. So various Layouts within a Flexible Content field type. Within each layout is one group where it pulls in the products, like so:

    https://www.dropbox.com/s/ae0zm3p1pg1ve7m/Screenshot%202021-02-19%20at%2018.59.18.png?dl=0

    So each choice relates to the Product added via the ACF Options repeater. These seem to populate nicely initially. Then when adding new products they are not shown as choices in the Post edit screen.

    I thought that changing ‘load_field’ to ‘prepare_field’ might do it, no such joy. I’m guessing I’ve done something wrong along the way?

    Thanks
    Lee

  • https://g.page/madeybfuse/review?rc

    Hi John, I initially used the ‘Dynamically populate a select field’s choices’ you mentioned above, I have updated the code I had, changing all instances of ‘load_field’ to ‘prepare_field’.

    Unfortunately, that didn’t work.

    Here is my code:

      // the following pulls in content from an options page into a select field, see here:
      // https://www.advancedcustomfields.com/resources/dynamically-populate-a-select-fields-choices/
    
      function acf_prepare_product_field_choices( $field ) {
        
        // reset choices
        $sub_field['products'] = array();
    
        // if has rows
        if( have_rows('product', 'option') ) {
            
            // while has rows
            while( have_rows('product', 'option') ) {
                
                // instantiate row
                the_row();
                
                
                // vars
                $value = get_sub_field('application_rate');
    			$label = get_sub_field('title');
    			$ar = get_sub_field('application_rate');
    
                
                // append to choices
                $sub_field['products'][ $value ] = $label;
                
            }
            
        }
        // return the field
        return $field;
    }
    
    add_filter('acf/prepare_field/name=products', 'acf_prepare_product_field_choices');

    I thought that changing ‘load_field’ to ‘prepare_field’ might do it, no such joy. I’m guessing I’ve done something wrong along the way?

    Thanks
    Lee

  • Hi John,

    I did try to post a reply here but it isn’t showing, I did place links to screenshots in the message, is that why?

    Thanks
    Lee

  • Sometimes things get marked as spam for various reasons.

    My understanding is that you have an options page with a repeater on it and you want to populated a select field with the values of that repeater.

    because select field is a sub field in a flex field you should use the field key for your filter.

    
    add_filter('acf/prepare_field/key=FIELD KY OF SELECT', 'populate_FIELD_NAME_from_options');
    function populate_FIELD_NAME_from_options($field) {
      $choices = array();
      if (have_rows('repeater_name', 'options') {
        while (have_rows('repeater_name', 'options') {
          the_row();
          $choices[get_sub_field('value_sub_field')] = $get_sub_field('label_sub_field');
        }
      }
      $field['choices'] = $choices;
      return $field;
    }
    
Viewing 7 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic.