Support

Account

Home Forums General Issues Dynamic population of select repeater prevents options from saving

Solving

Dynamic population of select repeater prevents options from saving

  • Hi there

    I couldn’t manage to find this issue logged on the forum already…

    I have managed to dynamically populate a select field as per the tutorial here: http://www.advancedcustomfields.com/resources/tutorials/dynamically-populate-a-select-fields-choices/

    This was altered as the select choices needed to be the titles of certain pages (children of parent page ID 8)

    // populate select repeater with restaurant names
     
        function my_acf_load_field( $field )
        {
            // reset choices
            $field['choices'] = array();
    
            // get restaurant locations (children of page id 8)
            $args = array(
                'post_type' => 'page',
                'post_parent' => 8,
                'orderby' => 'menu_order',
                'order' => 'ASC'
            );
    
            // The Query
            $the_query = new WP_Query( $args );
    
            // The Loop
            if ( $the_query->have_posts() ) {
                
                while ( $the_query->have_posts() ) {
                    $the_query->the_post();
                    $value = get_the_title();
                    $label = get_the_title();
                    $field['choices'][ $value ] = $label;
                }
            }
            /* Restore original Post Data */
            wp_reset_postdata();
    
            return $field;
        }
         
        // v4.0.0 and above
        add_filter('acf/load_field/name=restaurant_name', 'my_acf_load_field');

    Populating the select fields works great, as I’d hoped. The problem now is that on the ‘Edit Field Group’ page, my choices in the ‘Locations’ and ‘Options’ boxes are no longer saving. Once I disable the above function, the options are back again.

    Hope this makes sense, any help appreciated!

    Rich

  • This same thing is happening to me. Still searching through the forum to find solutions. Did you by chance ever get this working?

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

The topic ‘Dynamic population of select repeater prevents options from saving’ is closed to new replies.